This guide explains how to make the topology. The goal is like as below.
The topology after creating the network

Table of Contents

Step 1: Create the Network

To create the network, use the following command:

$ docker network create <network_name> -d bridge --subnet=<subnet>

To check whether it’s created or not, use the following command:

$ docker network ls

To remove networks, use the following command:

$ docker network rm <network_name> <network_name>

Example

To add a new network named nw20 whose subnet is 192.168.20.0/24, type like this:

$ docker network create nw20 -d bridge --subnet=192.168.20.0/24

To add a new network named nw21 whose subnet is 192.168.21.0/24, type like this:

$ docker network create nw21 -d bridge --subnet=192.168.21.0/24

The topology will like as below.
The topology after creating the network

Here is an example of adding a network named nw20 and nw21.

$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
f760eb82b30f        bridge              bridge              local
3a05f05787c9        host                host                local
3cd75e5b104d        none                null                local
$ docker network create nw20 -d bridge --subnet=192.168.20.0/24
d7531a6cd6a4ac8ba17a954ad0e51145a224d8ee1336b00fd627114ec63ab8d8
$ docker network create nw21 -d bridge --subnet=192.168.21.0/24
28af3fdd638c919c57d18054155b137280e57e8c3230b34936b86b3586a8319d
$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
f760eb82b30f        bridge              bridge              local
3a05f05787c9        host                host                local
3cd75e5b104d        none                null                local
d7531a6cd6a4        nw20                bridge              local
28af3fdd638c        nw21                bridge              local

Step 2: Create the Host (Container)

To create the host (Container), use the following command on the same directory as new makefile:

$ make create NAME=<container_name> NW=<network_name> IP=<ip address>

YOU CAN’T SPECIFY THE IP OF WHICH THE LAST BYTE IS ONE LIKE 192.168.20.1.
This is due to specification of the docker.

To check whether it’s created or not, use the following command:

$ docker container ls --all

To remove the host (Container), use the following command:

$ docker container rm <container_name>

Before this, you need to stop its container. Or you can use --force argument like this:

$ docker container rm <container_name> --force

Example

To create a container named R1 whose ip address is 192.168.20.4 whose network is nw20, type like this:

$ make create NAME=R1 NW=nw20 IP=192.168.20.4

To create a container named R2 whose ip address is 192.168.20.2 whose network is nw20, type like this:

$ make create NAME=R2 NW=nw20 IP=192.168.20.2

To create a container named R3 whose ip address is 192.168.20.3 whose network is nw20, type like this:

$ make create NAME=R3 NW=nw20 IP=192.168.20.3

The topology will like as below. This topology has already applied to Step 1. The topology after creating the host

Here is an example of creating and removing a container named R1 and R2 and R3.
The output depends on your OS.

$ make create NAME=R1 NW=nw20 IP=192.168.20.4
docker run -itd --name R1 --hostname=R1 --sysctl net.ipv4.ip_forward=1 --privileged -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix --net=nw20 --ip=192.168.20.4 ieng:2 /bin/bash
1b075d70e0bca58819c042bee8e6c40fc6dbfa4cf4a1ce6dd770b1b18ff24ba8
$ make create NAME=R2 NW=nw20 IP=192.168.20.2
docker run -itd --name R2 --hostname=R2 --sysctl net.ipv4.ip_forward=1 --privileged -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix --net=nw20 --ip=192.168.20.2 ieng:2 /bin/bash
d61974186b23fef44a67f4d94af2e9814bac34551c26c05798468bd3abd3b09b
$ make create NAME=R3 NW=nw20 IP=192.168.20.3
docker run -itd --name R3 --hostname=R3 --sysctl net.ipv4.ip_forward=1 --privileged -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix --net=nw20 --ip=192.168.20.3 ieng:2 /bin/bash
627e2a423a84e674deedd842555a
$ docker rm R1 R2 R3
Error response from daemon: You cannot remove a running container 1b075d70e0bca58819c042bee8e6c40fc6dbfa4cf4a1ce6dd770b1b18ff24ba8. Stop the container before attempting removal or force remove
Error response from daemon: You cannot remove a running container d61974186b23fef44a67f4d94af2e9814bac34551c26c05798468bd3abd3b09b. Stop the container before attempting removal or force remove
Error response from daemon: You cannot remove a running container 627e2a423a84e674deedd842555a3b5b9b628be84a66bd158292b4aeba7b5ced. Stop the container before attempting removal or force remove
$ docker stop R1 R2 R3
R1
R2
R3
$ docker rm R1 R2 R3
R1
R2
R3

As you can see, you have to stop the container before removing. But with --force argument, you can remove it.

Step 3: Connect between a host (Container) and a network

To connect between a host (Container) and a network, use the following command:

$ docker network connect <network_name> <container_name>

To disconnect between a host (Container) and a network, use the following command:

$ docker network disconnect <network_name> <container_name>

To check whether it’s connected or not, use the following command:

$ docker inspect <container_name>

You can also specify <network_name>, but you won’t check whether it’s connected by hostE or not.
The output is too long. It’s good idea to filter output to Networks by using grep.

To check an ip address or network interface, start and attach its container, and then use the following command:

$ ip a

Example

To connect between nw21 and R2, type like this:

$ docker network connect nw21 R2

To connect between nw21 and R3, type like this:

$ docker network connect nw21 R3

The topology will like as below. This topology has already applied to Step 1 and 2. The topology after connecting between the network and the container

Here is an example of connecting and checking (by ip a) between nw21 and R2 and nw21 and R3.

$ docker network connect nw21 R2
$ docker network connect nw21 R3
$ docker attach R2
ieng@R2:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 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
11: eth0@if12: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:14:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.20.2/24 brd 192.168.20.255 scope global eth0
       valid_lft forever preferred_lft forever
15: eth1@if16: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:15:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.21.2/24 brd 192.168.21.255 scope global eth1
       valid_lft forever preferred_lft forever
ieng@R2:~$ read escape sequence
$ docker attach R3
ieng@R3:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 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
13: eth0@if14: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:14:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.20.3/24 brd 192.168.20.255 scope global eth0
       valid_lft forever preferred_lft forever
17: eth1@if18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:15:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.21.3/24 brd 192.168.21.255 scope global eth1
       valid_lft forever preferred_lft forever
ieng@R3:~$ 

read escape sequence means I pressed CTRL - P and CTRL - Q (detaching the container).

See Also