Docker alternative to --network host on macOS and Windows
Asked Answered
S

1

7

I have two docker containers:

  • database
  • app that consumes the database

I run my database container like this:

docker run --name my-db -p 127.0.0.1:3306:3306 my-db-image

And my app container like this:

docker run --name my-app --network host -it my-app-image

This works fine on Linux. I can access the DB from both the host system and the app container. Perfect.

However --network host does not work on Mac and Windows:

The host networking driver only works on Linux hosts, and is not supported on Docker for Mac, Docker for Windows, or Docker EE for Windows Server.

(source: https://docs.docker.com/network/host/)

I can still access the database via 127.0.0.1:3306 from the main host, but I cannot access it from the app container.

How can I solve this issue? How can I let the app container connect to the database (and keep accessing also to the DB from the main host using 127.0.0.1:3306)?

I've tried using host.docker.internal and gateway.docker.internal but it doesn't work.

I've also tried to launch both containers using --network my-network after creating my-network with docker network create my-network but it doesn't work.

I can't figure out how to solve this issue.

Sympathin answered 13/1, 2019 at 1:51 Comment(9)
On Windows, I thought your docker containers would be assigned IP addresses (eg, 192.168.99.100?) which you would then use to connect from the host to the container, or I guess between containers...Navigable
Your docker network create my-network option should work as long as you refer to the db container by name from the app container (my-db:3306).Colloidal
it doesn't work, most likely my app needs a real address in order to workSympathin
Any reason you're not using docker-compose? Especially if these services will always run together. This allows for one file to run both services and create the network connecting them.Mcinerney
@BrianWagner no specific reason. It's my first time with docker, maybe you can suggest a docker compse file that fit my need ?Sympathin
You might find this answer I wrote yesterday to have some helpful pointers, especially if you're only using host networking to get an application container to talk to a database container.Arissa
Francesco, can you provide more info on how the app is accessing the DB? (I'm not familiar with --network host on Linux; and I guess I'd avoid it if it's so limited by OS.) You may need to manually open the ports. Or you may need to change the address the app is using to call 'my-network' and not 127.0.0.1' or 'localhost'.Mcinerney
@BrianWagner I actually solved my original issue using user-defined networks, initially they didn't work because the ports were different than the ones I used while having --network host. So I'm gonna accept your answer since it's the right solution to the general case.Sympathin
Francesco, I'm happy to edit the answer below, specifically the compose file, if it provides more clarity. Let me know if there is a detail to change or something to add.Mcinerney
M
4

For multiple services, it can often be easier to create a docker-compose.yml file that will launch all the services and any networks needed to connect them.

version: '3'
services:
  my-db:
    image: my-db-image
    ports:
      - "3306:3306"
    networks:
      - mynetwork

  my-app:
    image: my-app-image
    ports:
      - "8000:80"
    networks:
      - mynetwork

networks:
  mynetwork:

From the project folder, you run docker-compose up or docker-compose up -d to make the services run in the background.

In this scenario, the magic of Docker provisions a network with hostname "mynetwork". It should expose default ports to other services on that network. If you want to remap the ports, the pattern is target:source.

I don't know that you need the 'ports' config here. But I'm trying to map your config to the compose file. Also I'm assuming you need to expose the app on some port; using 8000 as it's pretty common setup.

What are the parameters here? Docker-compose reference

Mcinerney answered 13/1, 2019 at 2:21 Comment(1)
thanks, this looks indeed a good alternative than running the containers one by one. However, from the network perspective, isn't this equivalent to use --network mynetwork? Or it's somehow doing something more? Because I first need to solve the connectivity issue.Sympathin

© 2022 - 2024 — McMap. All rights reserved.