容器之间通信方式
1、–link
2、查看现有网络
docker network ls
居于现有网络创建容器
docker run -d -P --name t1 --net bridge busybox
docker run -d -P --name t2 --net bridge busybox
然后t1就可以ping t2 了。但是需要使用 t2 Ip 。使用别名不通
3、自己创建网络
docker network create mynet
然后运行容器
docker run -d -P --name t3 --net mynet busybox
docker run -d -P --name t4 --net mynet busybox
这样就可以使用别名ping 通
docker exec -it t3 tping t4
运行结果如下:
PING b2 (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.447 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.213 ms
64 bytes from 172.18.0.3: seq=2 ttl=64 time=0.193 ms
64 bytes from 172.18.0.3: seq=3 ttl=64 time=0.201 ms
64 bytes from 172.18.0.3: seq=4 ttl=64 time=0.353 ms
64 bytes from 172.18.0.3: seq=5 ttl=64 time=0.218 ms
64 bytes from 172.18.0.3: seq=6 ttl=64 time=0.214 ms
64 bytes from 172.18.0.3: seq=7 ttl=64 time=0.211 ms
64 bytes from 172.18.0.3: seq=8 ttl=64 time=0.198 ms
64 bytes from 172.18.0.3: seq=9 ttl=64 time=0.211 ms
64 bytes from 172.18.0.3: seq=10 ttl=64 time=0.206 ms
64 bytes from 172.18.0.3: seq=11 ttl=64 time=0.190 ms
64 bytes from 172.18.0.3: seq=12 ttl=64 time=0.218 ms
64 bytes from 172.18.0.3: seq=13 ttl=64 time=0.211 ms
64 bytes from 172.18.0.3: seq=14 ttl=64 time=0.212 ms
64 bytes from 172.18.0.3: seq=15 ttl=64 time=0.188 ms
64 bytes from 172.18.0.3: seq=16 ttl=64 time=0.202 ms
64 bytes from 172.18.0.3: seq=17 ttl=64 time=0.224 ms
64 bytes from 172.18.0.3: seq=18 ttl=64 time=0.313 ms
64 bytes from 172.18.0.3: seq=19 ttl=64 time=0.334 ms
64 bytes from 172.18.0.3: seq=20 ttl=64 time=0.214 ms
64 bytes from 172.18.0.3: seq=21 ttl=64 time=0.211 ms
64 bytes from 172.18.0.3: seq=22 ttl=64 time=0.213 ms
64 bytes from 172.18.0.3: seq=23 ttl=64 time=0.210 ms
64 bytes from 172.18.0.3: seq=24 ttl=64 time=0.215 ms
64 bytes from 172.18.0.3: seq=25 ttl=64 time=0.222 ms
64 bytes from 172.18.0.3: seq=26 ttl=64 time=0.210 ms
64 bytes from 172.18.0.3: seq=27 ttl=64 time=0.210 ms
|