How to attach a container to multiple network(multiple NIC card)
[root@ip-172-31-40-217 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
39450de642d6 bridge bridge local
f6345e9bd840 host host local
514a936c83c1 none null local
5e62d8b1f783 test bridge local
70b88d7ee77d test2 bridge local
[root@ip-172-31-40-217 ~]# docker container run -it --network bridge ubuntu:14.04 bash
root@581ec2e1364b:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:04
inet addr:172.17.0.4 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:656 (656.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
root@581ec2e1364b:/#
Now we have two NIC card(eth0 and eth1)
Now if you want to detach any network from your container then
[root@ip-172-31-40-217 ~]# docker network disconnect test 581ec2e1364b
[root@ip-172-31-40-217 ~]# docker container exec -it 581ec2e1364b bash
root@581ec2e1364b:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:04
inet addr:172.17.0.4 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:13 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1006 (1.0 KB) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
root@581ec2e1364b:/#
[root@ip-172-31-40-217 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
39450de642d6 bridge bridge local
f6345e9bd840 host host local
514a936c83c1 none null local
5e62d8b1f783 test bridge local
70b88d7ee77d test2 bridge local
[root@ip-172-31-40-217 ~]# docker container run -it --network bridge ubuntu:14.04 bash
root@581ec2e1364b:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:04
inet addr:172.17.0.4 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:656 (656.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
root@581ec2e1364b:/#
Now we are going connect test network also with our container
[root@ip-172-31-40-217 ~]# docker network connect test 581ec2e1364b
[root@ip-172-31-40-217 ~]# docker container exec -it 581ec2e1364b bash
root@581ec2e1364b:/# ipconfig
bash: ipconfig: command not found
root@581ec2e1364b:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:04
inet addr:172.17.0.4 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:12 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:936 (936.0 B) TX bytes:0 (0.0 B)
eth1 Link encap:Ethernet HWaddr 02:42:ac:12:00:02
inet addr:172.18.0.2 Bcast:172.18.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:13 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1046 (1.0 KB) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Now we have two NIC card(eth0 and eth1)
Now if you want to detach any network from your container then
[root@ip-172-31-40-217 ~]# docker network disconnect test 581ec2e1364b
[root@ip-172-31-40-217 ~]# docker container exec -it 581ec2e1364b bash
root@581ec2e1364b:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:04
inet addr:172.17.0.4 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:13 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1006 (1.0 KB) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
root@581ec2e1364b:/#
Now we have only one NIC card
-------------------------------------------------------------------------------------------------
If you want to delete any network then
[root@ip-172-31-40-217 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
39450de642d6 bridge bridge local
f6345e9bd840 host host local
514a936c83c1 none null local
5e62d8b1f783 test bridge local
70b88d7ee77d test2 bridge local
[root@ip-172-31-40-217 ~]# docker network rm test
test
[root@ip-172-31-40-217 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
39450de642d6 bridge bridge local
f6345e9bd840 host host local
514a936c83c1 none null local
70b88d7ee77d test2 bridge local
[root@ip-172-31-40-217 ~]#
To remove all networks not used by at least one container use the below command
[root@ip-172-31-40-217 ~]# docker network prune
WARNING! This will remove all networks not used by at least one container.
Are you sure you want to continue? [y/N] y
[root@ip-172-31-40-217 ~]#
[root@ip-172-31-40-217 ~]# docker network --help
Usage: docker network COMMAND
Manage networks
Commands:
connect Connect a container to a network
create Create a network
disconnect Disconnect a container from a network
inspect Display detailed information on one or more networks
ls List networks
prune Remove all unused networks
rm Remove one or more networks
Run 'docker network COMMAND --help' for more information on a command.
No comments:
Post a Comment