1.查看docker里有哪些网络类型,对应的命名空间
2.创建一个网络的命名空间叫lzc
3.创建2个centos7的容器,使用lzc这个网络命名空间
4.进入创建容器里面,查看下ip地址,互相ping下
5.创建一个使用默认的命名空间的容器,名字自己定义
6.进入新创建容器,ping使用lzc网络命名空间的容器,看是否可以ping通?
运行代码如下:
[root@docker ~]# docker network ls #查看docker里有哪些网络类型,对应的命名空间
NETWORK ID NAME DRIVER SCOPE
a4c72e45bdf4 bridge bridge local
14acf3f246ee chaochao bridge local
e6aa050bc769 host host local
9f11fcd6003d none null local
2f24912725f7 sc bridge local
[root@docker ~]# docker network create lzc #创建一个网络的命名空间叫lzc
395e378e3a86952f522c605b5000e24f0c721b35b883e3e33e30a148c8c20705
[root@docker ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
a4c72e45bdf4 bridge bridge local
14acf3f246ee chaochao bridge local
e6aa050bc769 host host local
395e378e3a86 lzc bridge local
9f11fcd6003d none null local
2f24912725f7 sc bridge local
[root@docker ~]# docker run -it -d --name lzc-centos-1 --network lzc -p 7001:7001 centos:7 #创建2个centos7的容器,使用lzc这个网络命名空间
c08240c5eb8b057ea030b61dd1bdf90ab99b7f00c9a0023a7eaebbeb069797ad
[root@docker ~]# docker run -it -d --name lzc-centos-2 --network lzc -p 7002:7002 centos:7 #创建2个centos7的容器,使用lzc这个网络命名空间
ac2918f6f40555228c8f496f395adce20e2607fb9cee55e8234f59e275b9ecc0
[root@docker ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
a4c72e45bdf4 bridge bridge local
14acf3f246ee chaochao bridge local
e6aa050bc769 host host local
395e378e3a86 lzc bridge local
9f11fcd6003d none null local
2f24912725f7 sc bridge local
[root@docker ~]# docker run -it -d --name chao-centos-01 --network lzc centos:7
36e380a6248a8c43edf6e66c1167fb76d7d1cf6d4fc9586aea27c9a62a75630f
[root@docker ~]# docker run -it -d --name chao-centos-02 --network lzc centos:7
0a244305b9185be5e5cdebf5229a6598f79f57ac2392fb5f41bea1e5019815f0
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0a244305b918 centos:7 "/bin/bash" 8 seconds ago Up 6 seconds chao-centos-02
36e380a6248a centos:7 "/bin/bash" 15 seconds ago Up 12 seconds chao-centos-01
ac2918f6f405 centos:7 "/bin/bash" 16 minutes ago Up 16 minutes 0.0.0.0:7002->7002/tcp, :::7002->7002/tcp lzc-centos-2
c08240c5eb8b centos:7 "/bin/bash" 16 minutes ago Up 16 minutes 0.0.0.0:7001->7001/tcp, :::7001->7001/tcp lzc-centos-1
cb9fd0eced6c nginx "/docker-entrypoint.…" 5 hours ago Up 5 hours 0.0.0.0:6677->80/tcp, :::6677->80/tcp chao-network-1.1
a7f87369ac1c nginx "/docker-entrypoint.…" 5 hours ago Up 5 hours 0.0.0.0:6666->80/tcp, :::6666->80/tcp chao-network-1
7123e3a3b5c1 nginx "/docker-entrypoint.…" 6 hours ago Up 6 hours 80/tcp chao-nginx-redis1
4f06900d4788 redis "docker-entrypoint.s…" 6 hours ago Up 6 hours 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp chao-redis-1
4cb9497e4b83 nginx:latest "/docker-entrypoint.…" 3 days ago Up 3 days 0.0.0.0:8002->80/tcp, :::8002->80/tcp chao-nginx2
e88ca98c52cb nginx:latest "/docker-entrypoint.…" 3 days ago Up 3 days 0.0.0.0:8001->80/tcp, :::8001->80/tcp chao-nginx1
2710b596777b nginx:latest "/docker-entrypoint.…" 3 days ago Up 3 days 0.0.0.0:8060->80/tcp, :::8060->80/tcp lizhichao-nginx
58d99df640b7 nginx "/docker-entrypoint.…" 4 days ago Up 4 days 0.0.0.0:8070->80/tcp, :::8070->80/tcp lizhichao6
da8640cb3a98 nginx "/docker-entrypoint.…" 4 days ago Up 4 days 80/tcp, 0.0.0.0:9000->90/tcp, :::9000->90/tcp lizhichao13
[root@docker ~]# docker exec -it chao-centos-01 /bin/bash #进入容器环境
[root@36e380a6248a /]# ip a #容器内有的命令无法使用,可以去查看容器详细信息
bash: ip: command not found
[root@docker containerd]# docker container inspect chao-centos-01|grep "IPAddress"
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.20.0.4",
[root@docker containerd]# docker container inspect chao-centos-02|grep "IPAddress"
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.20.0.5",
[root@docker containerd]#
[root@docker containerd]# docker exec -it chao-centos-01 /bin/bash #进入chao-centos-01环境
[root@36e380a6248a /]# ping 172.20.0.5 #chao-centos-01来ping另一个的IP地址
PING 172.20.0.5 (172.20.0.5) 56(84) bytes of data.
64 bytes from 172.20.0.5: icmp_seq=1 ttl=64 time=0.290 ms
64 bytes from 172.20.0.5: icmp_seq=2 ttl=64 time=0.120 ms
^C
--- 172.20.0.5 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1006ms
rtt min/avg/max/mdev = 0.120/0.205/0.290/0.085 ms
[root@36e380a6248a /]# ^C
[root@docker containerd]# docker exec -it chao-centos-02 /bin/bash #进入chao-centos-02环境
[root@0a244305b918 /]# ping 172.20.0.4 # #chao-centos-01来ping另一个的IP地址
PING 172.20.0.4 (172.20.0.4) 56(84) bytes of data.
64 bytes from 172.20.0.4: icmp_seq=1 ttl=64 time=0.165 ms
64 bytes from 172.20.0.4: icmp_seq=2 ttl=64 time=0.137 ms
^C
--- 172.20.0.4 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1011ms
rtt min/avg/max/mdev = 0.137/0.151/0.165/0.014 ms
[root@0a244305b918 /]#
[root@docker containerd]# docker run -it -d --name chaochao01 centos:7 #创建一个使用默认的命名空间的容器,名字自己定义
b5e9fa1b2001d161e06ebec63062264e2fded6a8413970d79724b3a2ab12d13a
[root@docker containerd]# docker exec -it chaochao01 /bin/bash #进入新创建容器,ping使用lzc网络命名空间的容器,看是否可以ping通
[root@b5e9fa1b2001 /]# ping 172.20.0.4
PING 172.20.0.4 (172.20.0.4) 56(84) bytes of data.
^C
--- 172.20.0.4 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1016ms
# ping不通