Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker container doesn't expose ports when --net=host is mentioned in the docker run command

I have a CentOS docker container on a CentOS docker host. When I use this command to run the docker image docker run -d --net=host -p 8777:8777 ceilometer:1.x the docker container get host's IP but doesn't have ports assigned to it.

If I run the same command without "--net=host" docker run -d -p 8777:8777 ceilometer:1.x docker exposes the ports but with a different IP. The docker version is 1.10.1. I want the docker container to have the same IP as the host with ports exposed. I also have mentioned in the Dockerfile the instruction EXPOSE 8777 but with no use when "--net=host" is mentioned in the docker run command.

like image 760
arevur Avatar asked Feb 23 '16 19:02

arevur


People also ask

Can Docker container access ports on host?

When running Docker natively on Linux, you can access host services using the IP address of the docker0 interface. From inside the container, this will be your default route. This would permit access to any ports on the host from Docker containers.

Can we expose a port on running container?

You cannot do this via Docker, but you can access the container's un-exposed port from the host machine.

Does Docker automatically open ports?

Published portsBy default, when you create or run a container using docker create or docker run , it does not publish any of its ports to the outside world. To make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, use the --publish or -p flag.


3 Answers

I was confused by this answer. Apparently my docker image should be reachable on port 8080. But it wasn't. Then I read

https://docs.docker.com/network/host/

To quote

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.

That's rather annoying as I'm on a Mac. The docker command should report an error rather than let me think it was meant to work.

Discussion on why it does not report an error

https://github.com/docker/for-mac/issues/2716

Not sure I'm convinced.

like image 136
Shane Gannon Avatar answered Oct 09 '22 22:10

Shane Gannon


The docker version is 1.10.1. I want the docker container to have same ip as the host with ports exposed.

When you use --net=host it tells the container to use the hosts networking stack. So you can't expose ports to the host, because it is the host (as far as the network stack is concerned).

docker inspect might not show the expose ports, but if you have an application listening on a port, it will be available as if it were running on the host.

like image 20
dnephin Avatar answered Oct 09 '22 22:10

dnephin


On Linux, I have always used --net=host when myapp needed to connect to an another docker container hosting PostgreSQL.

myapp reads an environment variable DATABASE in this example

Like Shane mentions this does not work on MacOS or Windows...

docker run -d -p 127.0.0.1:5432:5432 postgres:latest

So my app can't connect to my other other docker container:

docker run -e DATABASE=127.0.0.1:5432 --net=host myapp

To work around this, you can use host.docker.internal instead of 127.0.0.1 to resolve your hosts IP address.

Therefore, this works

docker run -e DATABASE=host.docker.internal:5432 -d myapp

Hope this saves someone time!

like image 4
rjdkolb Avatar answered Oct 09 '22 22:10

rjdkolb