经查阅 - 事实证明,它只是不允许"ping 容器名"这种方式。做个记录—
菜鸟教程是新建network,并使用"ping 容器名"的,所以就经常性的使用这种方式 > ping 容器名。
菜鸟教程,容器连接
官方解释:
Containers on the default bridge network can only access each other by IP addresses, unless you use the --link option, which is considered legacy. On a user-defined bridge network, containers can resolve each other by name or alias.
大概意思就是:默认的bridge network 只能通过ip的方式连接,除了…你可以使用–link选项。或者在自己定义的network上 才可以通过名字 命名的方式。官方地址:https://docs.docker.com/network/bridge/#differences-between-user-defined-bridges-and-the-default-bridge
使用ping ip的方式,先查看分配的ip。
docker network inspect bridge
这里就不尝试使用–link了,所以使用默认的network其实它也是互通的。
这里,可以新建自定义network,容器并使用该network,即可以使用"ping 容器名",这种方式了。
docker network ls
docker network create -d bridge net-connection
-d 采用哪种方式 bridge桥接,net-connection是自定义名字
在启动容器的时候,加上 --network net-connection, 即可
docker run -it --name test --network net-connection ubuntu:latest
安装 ping工具,bash: ping: command not found
apt install iputils-ping,发现报错
root@40ad4be4293d:/# ping 192.168.17.51
bash: ping: command not found
root@40ad4be4293d:/# apt install iputils-ping
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package iputils-ping
报错,执行更新apt-get update
root@40ad4be4293d:/# apt-get update
Get:1 http://deb.debian.org/debian buster InRelease [121 kB]
Get:2 http://deb.debian.org/debian buster-updates InRelease [51.9 kB]
Get:3 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]
Get:4 http://deb.debian.org/debian buster/main amd64 Packages [7907 kB]
Get:5 http://security.debian.org/debian-security buster/updates/main amd64 Packages [289 kB]
Get:6 http://deb.debian.org/debian buster-updates/main amd64 Packages [10.9 kB]
Get:7 http://repo.mysql.com/apt/debian buster InRelease [21.5 kB]
Get:8 http://repo.mysql.com/apt/debian buster/mysql-8.0 amd64 Packages [6017 B] Fetched 8473 kB in 9s (933 kB/s)
Reading package lists... Done
root@40ad4be4293d:/#
再重新安装
apt install iputils-ping
安装完后,即可以使用ping啦