Skip to content

Docker Network Commands

Alexandru Dan edited this page Nov 6, 2017 · 1 revision

Example

Create two containers. By default both are connected to bridge network.

$ docker run -itd --name=container1 busybox
$ docker run -itd --name=container2 busybox

Create an isolated bridge network.

$ docker network create -d bridge --subnet 172.25.0.0/16 isolated_nw

Connect container2 to isolated network.Notice that container2 is assigned an IP address automatically. Because you specified a --subnet when creating the network, the IP address was chosen from that subnet.

$ docker network connect isolated_nw container2

Start a third container, but this time assign it an IP address using the --ip flag and connect it to the isolated_nw network using the docker run command’s --network option. Because you connected container3 to the isolated_nw when you started it, it is not connected to the default bridge network at all.

$ docker run --network=isolated_nw --ip=172.25.3.3 -itd --name=container3 busybox

Currently, container2 is attached to both bridge and isolated_nw networks, so it can communicate with both container1 and container3. However, container3 and container1 do not have any networks in common, so they cannot communicate. To verify this, attach to container3 and try to ping container1 by IP address.

$ docker attach container3

$ ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2): 56 data bytes
^C

--- 172.17.0.2 ping statistics ---
10 packets transmitted, 0 packets received, 100% packet loss

Clone this wiki locally