在Docker中每一个容器都是一个独立的个体,相互之间不能进行信息的传输。这里学习一种让容器可以相互联系的方法——自定义网络。
一、创建自定义网络
命令:
docker network create --driver 网络模式 --subnet 子网地址 --gateway 网关 网络名称
测试:
--driver bridge 这里是以创建的是一个桥接网络,也是默认的配置
[root@VM-8-4-centos idea]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 jmsNet
6dbab1b0436de2e42ba992e8e6f7c6e11630ac895de89b705fdb111fbb12262b
[root@VM-8-4-centos idea]# docker network ls
NETWORK ID NAME DRIVER SCOPE
105fe3d66e8a bridge bridge local
e0caa1a9d935 host host local
6dbab1b0436d jmsNet bridge local
de1fb1960308 none null local
二、在此网络下创建两个tomcat的容器
[root@VM-8-4-centos /]# docker run -d --name Tomcat02 --net jmsNet mytomcat:2.0 739f03429a180c331bd398b9ec24f184d316852fa960303c5952b874c94fd955 [root@VM-8-4-centos /]# docker run -d --name Tomcat01 --net jmsNet mytomcat:2.0 eb58633cdce398584b1a8985f6b6623b047a242886b3a359bd0c462d9f1ef88d [root@VM-8-4-centos /]# docker exec -it Tomcat01 ping Tomcat02 PING Tomcat02 (192.168.0.2): 56 data bytes 64 bytes from 192.168.0.2: icmp_seq=0 ttl=64 time=0.099 ms 64 bytes from 192.168.0.2: icmp_seq=1 ttl=64 time=0.082 ms 64 bytes from 192.168.0.2: icmp_seq=2 ttl=64 time=0.067 ms 64 bytes from 192.168.0.2: icmp_seq=3 ttl=64 time=0.073 ms 64 bytes from 192.168.0.2: icmp_seq=4 ttl=64 time=0.072 ms 5 packets transmitted, 5 packets received, 0% packet loss round-trip min/avg/max/stddev = 0.067/0.079/0.099/0.000 ms [root@VM-8-4-centos /]# docker exec -it Tomcat02 ping Tomcat01 PING Tomcat01 (192.168.0.3): 56 data bytes 64 bytes from 192.168.0.3: icmp_seq=0 ttl=64 time=0.070 ms 64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.101 ms 64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.068 ms 64 bytes from 192.168.0.3: icmp_seq=3 ttl=64 time=0.075 ms 64 bytes from 192.168.0.3: icmp_seq=4 ttl=64 time=0.078 ms 64 bytes from 192.168.0.3: icmp_seq=5 ttl=64 time=0.089 ms 6 packets transmitted, 6 packets received, 0% packet loss round-trip min/avg/max/stddev = 0.068/0.080/0.101/0.000 ms
两个容器可以互相ping通,证明互联成功
(本文仅作个人学习记录用,如有纰漏敬请指正)
标签:互联,自定义,--,bytes,192.168,64,ms,ttl,Docker From: https://www.cnblogs.com/jmsstudy/p/17466917.html