[root@liuxin-test01 ~]# ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ca:41:84 brd ff:ff:ff:ff:ff:ff
inet 192.168.8.192/24 brd 192.168.8.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:feca:4184/64 scope link
valid_lft forever preferred_lft forever
3: eth1: mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 00:0c:29:ca:41:8e brd ff:ff:ff:ff:ff:ff
4: docker0: mtu 1500 qdisc noqueue state DOWN
link/ether 02:42:a3:f4:2f:40 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:a3ff:fef4:2f40/64 scope link
valid_lft forever preferred_lft forever
--rm 参数讲解一下:
在Docker容器退出时,默认容器内部的文件系统仍然被保留,以方便调试并保留用户数据。
但是,对于foreground容器,由于其只是在开发调试过程中短期运行,其用户数据并无保留的必要,因而可以在容器启动时设置--rm选项,这样在容器退出时就能够自动清理容器内部的文件系统。
--net=none 无网络环境
--net=bridge 默认的参数,通过网桥(docker0)来设置容器的网络。也可以通过DOCKER_OPTS选项的-b参数来指定默认网桥
--net=host 共享主机的网络环境,不推荐这么设置
--net=container 两个容器共享IP地址和端口号等网络资源
下面两个容器是在两个终端中创建的
目前创新互联建站已为上千余家的企业提供了网站建设、域名、雅安服务器托管、网站托管运营、企业网站设计、恒山网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
[root@liuxin-test01 ~]# docker run --rm -it --net=none --name='centos01' centos:7.4.1708
[root@f64cdc7ffff1 /]#
[root@liuxin-test01 ~]# docker run --rm -it --net=none --name='centos02' centos:7.4.1708
[root@cd4df383b68e /]#
[root@liuxin-test01 ~]# docker inspect -f '{{.State.Pid}}' f64
21682
[root@liuxin-test01 ~]# docker inspect -f '{{.State.Pid}}' cd4
21832
[root@liuxin-test01 ~]# mkdir -p /var/run/netns
[root@liuxin-test01 ~]# ln -s /proc/21682/ns/net /var/run/netns/21682
[root@liuxin-test01 ~]# ln -s /proc/21832/ns/net /var/run/netns/21832
[root@liuxin-test01 ~]#
veth 是虚拟的以太设备,类似于网卡设备。这个是linux容器技术引进的,要求必须成对出现
[root@liuxin-test01 ~]# ip link add A type veth peer name B
[root@liuxin-test01 ~]# ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ca:41:84 brd ff:ff:ff:ff:ff:ff
inet 192.168.8.192/24 brd 192.168.8.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:feca:4184/64 scope link
valid_lft forever preferred_lft forever
3: eth1: mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 00:0c:29:ca:41:8e brd ff:ff:ff:ff:ff:ff
4: docker0: mtu 1500 qdisc noqueue state DOWN
link/ether 02:42:a3:f4:2f:40 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:a3ff:fef4:2f40/64 scope link
valid_lft forever preferred_lft forever
157: B@A: mtu 1500 qdisc noop state DOWN qlen 1000
link/ether de:f7:3b:24:a5:0e brd ff:ff:ff:ff:ff:ff
158: A@B: mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 9a:65:96:de:04:90 brd ff:ff:ff:ff:ff:ff
我们可以看到,加入到容器之后,再次执行ip a 已经看不到这两个设备了
[root@liuxin-test01 ~]# ip link set A netns 21682
[root@liuxin-test01 ~]# ip link set B netns 21832
[root@liuxin-test01 ~]# ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ca:41:84 brd ff:ff:ff:ff:ff:ff
inet 192.168.8.192/24 brd 192.168.8.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:feca:4184/64 scope link
valid_lft forever preferred_lft forever
3: eth1: mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 00:0c:29:ca:41:8e brd ff:ff:ff:ff:ff:ff
4: docker0: mtu 1500 qdisc noqueue state DOWN
link/ether 02:42:a3:f4:2f:40 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:a3ff:fef4:2f40/64 scope link
valid_lft forever preferred_lft forever
设置两个容器网络空间的ip
[root@liuxin-test01 ~]# ip netns exec 21682 ip addr add 192.168.99.1/32 dev A
[root@liuxin-test01 ~]# ip netns exec 21832 ip addr add 192.168.99.2/32 dev B
[root@liuxin-test01 ~]# ip netns exec 21682 ip link set A up
[root@liuxin-test01 ~]# ip netns exec 21832 ip link set B up
[root@liuxin-test01 ~]# ip netns exec 21682 ip route add 192.168.99.2/32 dev A
[root@liuxin-test01 ~]# ip netns exec 21832 ip route add 192.168.99.1/32 dev B
[root@f64cdc7ffff1 /]# ping 192.168.99.2
PING 192.168.99.2 (192.168.99.2) 56(84) bytes of data.
64 bytes from 192.168.99.2: icmp_seq=1 ttl=64 time=0.095 ms
[root@cd4df383b68e /]# ping 192.168.99.1
PING 192.168.99.1 (192.168.99.1) 56(84) bytes of data.
64 bytes from 192.168.99.1: icmp_seq=1 ttl=64 time=0.057 ms
文章名称:部署docker之点到点的容器网络
网页路径:http://www.mswzjz.cn/qtweb/news31/489181.html
攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能