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
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: