6

How to tell docker on mac to use host network for all purpose such that my docker container can connect to any service running on host machine via localhost or 127.0.0.1 ?

Ran docker run --net=host -it myimage and the container cannot connect to anything running on host machine via localhost. I get connection refused error.

docker version
Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   9ee9f40
 Built:        Thu Apr 26 07:13:02 2018
 OS/Arch:      darwin/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.1-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   9ee9f40
  Built:        Thu Apr 26 07:22:38 2018
  OS/Arch:      linux/amd64
  Experimental: true
namokarm
  • 668
  • 1
  • 7
  • 20
user1870400
  • 6,028
  • 13
  • 54
  • 115

2 Answers2

12

The host network driver will work on Linux the way you'd expect, but it will not give you what you are looking for on Mac and Windows.

It's mentioned here in the official docs: https://docs.docker.com/network/network-tutorial-host/.

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.

You can start your containers with --network host on the Mac without an error, but you'll not get the results you'd expect.

This is because the Docker for Mac and Windows applications use a virtual machine under the hood, and "host" means the VM in this case, not your host machine.

If you are wondering whether Docker will implement this on Mac and Windows follow this issue https://github.com/docker/for-mac/issues/2716.

You can read more about the topic on the Docker forums here and here.

To complete the picture:

  • if you are developing a regular web application, please use -p, or it's better to look into user defined networks, docker-compose, and Swarm or Kubernetes so that you build your app with the end in mind.
  • if you are in a special situation and will deploy your app with --network host in production, for whatever reason, it's best to develop on a Linux machine.
dev.e.loper
  • 35,446
  • 76
  • 161
  • 247
takacsmark
  • 3,933
  • 23
  • 26
  • The container doesn't even start when I issue --network host or --net=host. Is there any workaround possible? – user1870400 Jun 14 '18 at 14:45
  • I updated the answer, but the short answer is to use -p. Is there any special requirement why you need this? – takacsmark Jun 14 '18 at 14:46
  • You can also use `-P, --publish-all Publish all exposed ports to random ports` – takacsmark Jun 14 '18 at 14:48
  • -P wouldn't quite work for me because I should have all the processes that I want to communicate in the host to be running inside the container. My goal here is I want my app container to be talking to say a database or databases on the host machine. – user1870400 Jun 14 '18 at 15:04
  • 1
    In this case try to use the hostname `host.docker.internal` as described here https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – takacsmark Jun 14 '18 at 16:15
0

Docker’s host networking feature is not supported on Mac, and interestingly docker run command does not complain about it.

So from host if you would try for localhost:port for e.g. nginx on port 80, you would get the error where as same would work on linux.

Mac would have the following error:

$ curl localhost:80
curl: (7) Failed to connect to localhost port 80: Connection refused
Deepak Singhvi
  • 727
  • 6
  • 13