In the previous class, we’ve used the following network topology. Current Topology

This guide explains how to extend the topology.

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 nw59 whose subnet is 192.168.59.0/24, type like this:

$ docker network create nw59 -d bridge --subnet=192.168.59.0/24

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

Here is an example of adding and removing a network named nw59.

$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
a1aea284a91a        bridge              bridge              local
3a05f05787c9        host                host                local
3cd75e5b104d        none                null                local
6361b594a881        nw56                bridge              local
d98813a74c43        nw57                bridge              local
d9e438cba1b3        nw58                bridge              local
$ docker network create nw59 -d bridge --subnet=192.168.59.0/24
7a38c2a32e90bc7fa8a3974f9cbc119cb8031461e56144e387ad1dbeb5fdfb99
$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
a1aea284a91a        bridge              bridge              local
3a05f05787c9        host                host                local
3cd75e5b104d        none                null                local
6361b594a881        nw56                bridge              local
d98813a74c43        nw57                bridge              local
d9e438cba1b3        nw58                bridge              local
7a38c2a32e90        nw59                bridge              local
$ docker network rm nw59
nw59
$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
a1aea284a91a        bridge              bridge              local
3a05f05787c9        host                host                local
3cd75e5b104d        none                null                local
6361b594a881        nw56                bridge              local
d98813a74c43        nw57                bridge              local
d9e438cba1b3        nw58                bridge              local

Step 2: Create the Host (Container)

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

$ docker create -it --name=<container_name> --net=<network_name> --cap-add=NET_ADMIN --ip=<ip address> ieng /bin/bash

After that, you need to start its container.
YOU CAN’T SPECIFY THE IP OF WHICH THE LAST BYTE IS ONE LIKE 192.168.59.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 hostE whose ip address is 192.168.59.2 whose network is nw59, type like this:

$ docker create -it --name=hostE --net=nw59 --cap-add=NET_ADMIN --ip=192.168.59.2 ieng /bin/bash

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 attaching and detaching and removing a container named hostE.

$ docker container ls --all
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                  PORTS               NAMES
f894d04b5e7e        ieng                "/bin/bash"         6 days ago          Exited (0) 6 days ago                       hostD
d39fdce4f635        ieng                "/bin/bash"         6 days ago          Exited (0) 6 days ago                       hostC
b78f6d49437c        ieng                "/bin/bash"         6 days ago          Exited (0) 6 days ago                       hostB
bb46dbffbfad        ieng                "/bin/bash"         6 days ago          Exited (0) 6 days ago                       hostA
$ docker create -it --name=hostE --net=nw59 --cap-add=NET_ADMIN --ip=192.168.59.2 ieng /bin/bash
11873b72f6391a9e797092b09f61f5967f49e1610e7d682a44d706a9d5011959
$ docker container ls --all
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                  PORTS               NAMES
11873b72f639        ieng                "/bin/bash"         4 seconds ago       Created                                     hostE
f894d04b5e7e        ieng                "/bin/bash"         6 days ago          Exited (0) 6 days ago                       hostD
d39fdce4f635        ieng                "/bin/bash"         6 days ago          Exited (0) 6 days ago                       hostC
b78f6d49437c        ieng                "/bin/bash"         6 days ago          Exited (0) 6 days ago                       hostB
bb46dbffbfad        ieng                "/bin/bash"         6 days ago          Exited (0) 6 days ago                       hostA
$ docker start hostE
hostE
$ docker attach hostE
ieng@11873b72f639:~$ read escape sequence
$ docker rm hostE
Error response from daemon: You cannot remove a running container 11873b72f6391a9e797092b09f61f5967f49e1610e7d682a44d706a9d5011959. Stop the container before attempting removal or force remove
$ docker stop hostE
hostE
$ docker rm hostE
hostE

read escape sequence means I pressed CTRL - P and CTRL - Q (detaching the container).
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 nw59 and hostB, type like this:

$ docker network connect nw59 hostB

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 docker inspect and ip a) and disconnecting between nw59 and hostB.

$ docker start hostB
hostB
$ docker attach hostB
ieng@b78f6d49437c:~$ 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
14: eth1@if15: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:3a:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.58.2/24 brd 192.168.58.255 scope global eth1
       valid_lft forever preferred_lft forever
18: eth0@if19: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:38:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.56.3/24 brd 192.168.56.255 scope global eth0
       valid_lft forever preferred_lft forever
ieng@b78f6d49437c:~$ read escape sequence
$ docker inspect hostB |  grep Networks -A 100
            "Networks": {
                "nw56": {
                    "IPAMConfig": {
                        "IPv4Address": "192.168.56.3"
                    },
                    "Links": null,
                    "Aliases": [
                        "b78f6d49437c"
                    ],
                    "NetworkID": "6361b594a881ead174967e117fe821ddfc9c6085946282b46fa45b789da48b15",
                    "EndpointID": "11fa755819a59d75f30a0e14dc837aed6d4c073b659f457c335de00f2a7c25b4",
                    "Gateway": "192.168.56.1",
                    "IPAddress": "192.168.56.3",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:c0:a8:38:03",
                    "DriverOpts": null
                },
                "nw58": {
                    "IPAMConfig": {
                        "IPv4Address": "192.168.58.2"
                    },
                    "Links": null,
                    "Aliases": [
                        "b78f6d49437c"
                    ],
                    "NetworkID": "d9e438cba1b35a0d37cd167fa572a4bf8bbf896592fcff7e52c843c55ef18316",
                    "EndpointID": "ad97fc0248583ba0c027cd04d202db5f5dd6c5b53d3229acfab1327f03819cc1",
                    "Gateway": "192.168.58.1",
                    "IPAddress": "192.168.58.2",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:c0:a8:3a:02",
                    "DriverOpts": null
                }
            }
        }
    }
]
$ docker network connect nw59 hostB
$ docker inspect hostB |  grep Networks -A 100
            "Networks": {
                "nw56": {
                    "IPAMConfig": {
                        "IPv4Address": "192.168.56.3"
                    },
                    "Links": null,
                    "Aliases": [
                        "b78f6d49437c"
                    ],
                    "NetworkID": "6361b594a881ead174967e117fe821ddfc9c6085946282b46fa45b789da48b15",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                },
                "nw58": {
                    "IPAMConfig": {
                        "IPv4Address": "192.168.58.2"
                    },
                    "Links": null,
                    "Aliases": [
                        "b78f6d49437c"
                    ],
                    "NetworkID": "d9e438cba1b35a0d37cd167fa572a4bf8bbf896592fcff7e52c843c55ef18316",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                },
                "nw59": {
                    "IPAMConfig": {},
                    "Links": null,
                    "Aliases": [
                        "b78f6d49437c"
                    ],
                    "NetworkID": "",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }
        }
    }
]
$ docker attach hostB
ieng@b78f6d49437c:~$ 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
14: eth1@if15: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:3a:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.58.2/24 brd 192.168.58.255 scope global eth1
       valid_lft forever preferred_lft forever
16: eth2@if17: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:3b:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.59.3/24 brd 192.168.59.255 scope global eth2
       valid_lft forever preferred_lft forever
18: eth0@if19: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:38:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.56.3/24 brd 192.168.56.255 scope global eth0
       valid_lft forever preferred_lft forever
ieng@b78f6d49437c:~$ read escape sequence
$ docker network disconnect nw59 hostB
$ docker inspect hostB |  grep Networks -A 100
            "Networks": {
                "nw56": {
                    "IPAMConfig": {
                        "IPv4Address": "192.168.56.3"
                    },
                    "Links": null,
                    "Aliases": [
                        "b78f6d49437c"
                    ],
                    "NetworkID": "6361b594a881ead174967e117fe821ddfc9c6085946282b46fa45b789da48b15",
                    "EndpointID": "11fa755819a59d75f30a0e14dc837aed6d4c073b659f457c335de00f2a7c25b4",
                    "Gateway": "192.168.56.1",
                    "IPAddress": "192.168.56.3",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:c0:a8:38:03",
                    "DriverOpts": null
                },
                "nw58": {
                    "IPAMConfig": {
                        "IPv4Address": "192.168.58.2"
                    },
                    "Links": null,
                    "Aliases": [
                        "b78f6d49437c"
                    ],
                    "NetworkID": "d9e438cba1b35a0d37cd167fa572a4bf8bbf896592fcff7e52c843c55ef18316",
                    "EndpointID": "ad97fc0248583ba0c027cd04d202db5f5dd6c5b53d3229acfab1327f03819cc1",
                    "Gateway": "192.168.58.1",
                    "IPAddress": "192.168.58.2",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:c0:a8:3a:02",
                    "DriverOpts": null
                }
            }
        }
    }
]
$ docker attach hostB
ieng@b78f6d49437c:~$ 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
14: eth1@if15: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:3a:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.58.2/24 brd 192.168.58.255 scope global eth1
       valid_lft forever preferred_lft forever
18: eth0@if19: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:38:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.56.3/24 brd 192.168.56.255 scope global eth0
       valid_lft forever preferred_lft forever

Therefore, we got to know an ip address (192.168.59.3) and a network interface name (eth2) of hostB’s nw59. The topology after connecting between the network and the container