1 2 3 4 5 6 7 8 9 10 11 12 |
[root@docker1 bin]# vi linkns #!/usr/bin/bash echo The script will link all running containers namespace into /var/run/netns, so you can use ip netns command for them. echo Deleting all existed symbol links rm -f /var/run/netns/* for i in `docker container ls | awk {'print $NF'} | grep -v NAMES` do dockerpid=`docker inspect -f {{'.State.Pid'}} $i` ln -s /proc/$dockerpid/ns/net /var/run/netns/$i echo namespace for ${i} was linked into /var/run/netns/${i} done |
运行以上脚本即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
[root@docker1 bin]# linkns The script will link all running containers namespace into /var/run/netns, so you can use ip netns command for them. Deleting all existed symbol links namespace for busybox-2 was linked into /var/run/netns/busybox-2 namespace for busybox-1 was linked into /var/run/netns/busybox-1 [root@docker1 bin]# ip netns show busybox-1 (id: 0) busybox-2 (id: 1) [root@docker1 bin]# ip netns exec busybox-1 ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 0.0.0.0 ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 16 bytes 1096 (1.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4 bytes 280 (280.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@docker1 bin]# ip netns exec busybox-2 ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.3 netmask 255.255.0.0 broadcast 0.0.0.0 ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet) RX packets 15 bytes 1054 (1.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2 bytes 140 (140.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 |