![](/img/trans.png)
[英]Can not ping docker container from another host which is in the same LAN
[英]Cannot ping docker container from the host machine
我从docker image centos:7
创建了一个docker容器,并通过以下命令启动了它:
docker run -h centos -it CONTAINER_ID
您可以看到我给它指定了主机名centos
,因此如果我从主机上ping docker容器,它应该可以工作。
ping centos
好吧,那没有用。 我收到以下错误。
ping: centos: Name or service not known
但是,如果我将主机名更改为Docker容器的IP地址,则可以使用。
> ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.041 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.042 ms
64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.037 ms
由于ip地址不如主机名可靠,因此我想知道如何通过主机名ping我的Docker容器。
如果要通过主机名访问容器,则需要在/etc/hosts
添加相应的IP。 由于Docker不修改主机上的任何内容,因此您必须手动执行此操作。
echo "$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' centos) centos" >> /etc/hosts
相反,我建议在主机上映射所需的容器端口(使用-p <local-port>:<container-port>
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.