Docker容器启动默认使用bridge的网络,容器之间不能ping通??

经查阅 - 事实证明,它只是不允许"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啦

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值