Docker Networking (Connect, Disconnect)
In this guide, we will see how we can connect multiple networks to a container and how to disconnect them.
Create a container with the bridge network-
gaurav@learning-ocean:~$ docker container run -it --network bridge ubuntu:14.04 bash
root@1a5752e79f47:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:02
inet addr:172.17.0.2 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:7 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:606 (606.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@1a5752e79f47:/#
It has been allocated with eht0 NIC. If we want to allocate it with one more NIC interface, create one more network with
Let's create a network with name ‘test’-
gaurav@learning-ocean:~$ docker network create test
348f7295d3cac9a633c0a8619879a55c0371691c454139f3b9f3f5e7693a239d
By default, a bridge network is created.
gaurav@learning-ocean:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
6c51373f78ac bridge bridge local
3a33f83c3663 host host local
e4ebd601732c none null local
348f7295d3ca test bridge local
gaurav@learning-ocean:~$
To attach this network to the container, use below command -
$ docker network connect <network_name> <container_name>
example:
gaurav@learning-ocean:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a5752e79f47 ubuntu:14.04 "bash" 3 minutes ago Up 23 seconds focused_heisenberg
gaurav@learning-ocean:~$ docker network connect test 1a5752e79f47
gaurav@learning-ocean:~$
Another network has been attached to the container with name as eth1-
gaurav@learning-ocean:~$ docker container exec -it 1a bash
root@1a5752e79f47:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:02
inet addr:172.17.0.2 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:976 (976.0 B) TX bytes:0 (0.0 B)
eth2 Link encap:Ethernet HWaddr 02:42:ac:13:00:02
inet addr:172.19.0.2 Bcast:172.19.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:11 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:906 (906.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@1a5752e79f47:/#
Now to disconnect this network, you can simply replace the connect with disconnect-
$ docker network disconnect <network_name> <container_name>
Example:
gaurav@learning-ocean:~$ docker network disconnect test 1a5752e79f47
gaurav@learning-ocean:~$
Verifying the same by running ifconfig command on the container-
gaurav@learning-ocean:~$ docker container exec -it 1a bash
root@1a5752e79f47:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:02
inet addr:172.17.0.2 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:976 (976.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@1a5752e79f47:/#
Note: Connecting a container with none network with another network
You cannot connect a container with none network with another network. You will below error even you try to-
Error response from daemon: Container cannot be connected to multiple networks with one of the networks in private (none) mode