且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在docker运行命令中提到了--net = host时,Docker容器不会暴露端口

更新时间:2023-08-20 21:46:22


docker版本是1.10.1。我希望docker容器与暴露端口的主机具有相同的IP。


当您使用 net = host 它告诉容器使用主机网络堆栈。因此,您不能将端口暴露给主机,因为它主机(就网络堆栈而言)。



docker inspect 可能不会显示公开端口,但是如果您有一个应用程序在端口上侦听,它将可用,就好像它在主持人。


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.

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.