Ubuntu 22.04
Sponsored Link

Docker : Docker Network2022/09/08

 
This is the basic usage to configure Docker Network.
[1] When running containers without specifying network, default [bridge] network is assigned.
# display network list

root@dlp:~#
docker network ls

NETWORK ID     NAME           DRIVER    SCOPE
c6827e510929   bridge         bridge    local
0a92932bfe22   host           host      local
ee71731d3229   none           null      local
0e12d0d715f2   root_default   bridge    local

# display details of [bridge]

root@dlp:~#
docker network inspect bridge

[
    {
        "Name": "bridge",
        "Id": "c6827e510929a2c9cd6c3ca8337488607a4055f01e8ac18ea067baf8ae1ce88f",
        "Created": "2022-09-08T01:31:28.575826114Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

# [bridge] is assigned as container network by default

root@dlp:~#
docker run ubuntu /bin/bash -c "apt-get update; apt-get -y install iproute2; /usr/sbin/ip route"

.....
.....
default via 172.17.0.1 dev eth0
172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.2

root@dlp:~#
docker ps -a | head -2

CONTAINER ID   IMAGE                      COMMAND                  CREATED              STATUS                          PORTS     NAMES
1c4d8a95eade   ubuntu                     "/bin/bash -c 'apt-g…"   About a minute ago   Exited (0) About a minute ago             stoic_colden
root@dlp:~#
docker commit 1c4d8a95eade srv.world/ubuntu-iproute

[2] If you'd like to assign another network, set like follows.
# create network [network01] with [192.168.100.0/24] subnet

root@dlp:~#
docker network create --subnet 192.168.100.0/24 network01

b1c995fda78a54c2ff2bb0ec441fd5a992d2bc3b370a3b61a82233eb06cc4252

root@dlp:~#
docker network ls

NETWORK ID     NAME           DRIVER    SCOPE
c6827e510929   bridge         bridge    local
0a92932bfe22   host           host      local
b1c995fda78a   network01      bridge    local
ee71731d3229   none           null      local
0e12d0d715f2   root_default   bridge    local

# run a container with specifying [network01]

root@dlp:~#
docker run --net network01 srv.world/ubuntu-iproute /usr/sbin/ip route

default via 192.168.100.1 dev eth0
192.168.100.0/24 dev eth0 proto kernel scope link src 192.168.100.2

# to attach the network to existing running container, set like follows

root@dlp:~#
docker ps

CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS         PORTS                                   NAMES
679b88eb61d1   srv.world/ubuntu-nginx   "/usr/sbin/nginx -g …"   3 seconds ago   Up 2 seconds   0.0.0.0:8081->80/tcp, :::8081->80/tcp   dazzling_mayer

root@dlp:~#
docker exec 679b88eb61d1 /usr/sbin/ip route

default via 172.17.0.1 dev eth0
172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.2

# attach network to specify an IP address in the subnet

root@dlp:~#
docker network connect --ip 192.168.100.10 network01 679b88eb61d1
root@dlp:~#
docker exec 679b88eb61d1 /usr/sbin/ip route

default via 172.17.0.1 dev eth0
172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.2
192.168.100.0/24 dev eth1 proto kernel scope link src 192.168.100.10

# to disconnect the network, set like follows

root@dlp:~#
docker network disconnect network01 679b88eb61d1

root@dlp:~#
docker exec 679b88eb61d1 /usr/sbin/ip route

default via 172.17.0.1 dev eth0
172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.2
[3] To remove docker networks, set like follows.
root@dlp:~#
docker network ls

NETWORK ID     NAME           DRIVER    SCOPE
c6827e510929   bridge         bridge    local
0a92932bfe22   host           host      local
b1c995fda78a   network01      bridge    local
ee71731d3229   none           null      local
0e12d0d715f2   root_default   bridge    local

# remove [network01]

root@dlp:~#
docker network rm network01

network01
# remove networks which containers don't use at all

root@dlp:~#
docker network prune

WARNING! This will remove all custom networks not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Networks:
root_default
[4] To connect to Host network, set like follows.
root@dlp:~#
docker network ls

NETWORK ID     NAME      DRIVER    SCOPE
c6827e510929   bridge    bridge    local
0a92932bfe22   host      host      local
ee71731d3229   none      null      local

root@dlp:~#
docker images

REPOSITORY                  TAG           IMAGE ID       CREATED         SIZE
srv.world/ubuntu-iproute    latest        ddf1e2b3acd2   6 minutes ago   119MB
srv.world/ubuntu-apache2    latest        b7891f747bd5   18 hours ago    227MB
srv.world/ubuntu-nginx      latest        51f5190bb9d9   18 hours ago    170MB
dlp.srv.world:5000/ubuntu   my-registry   2dc39ba059dc   6 days ago      77.8MB
ubuntu                      latest        2dc39ba059dc   6 days ago      77.8MB

# run a container with [host] network

root@dlp:~#
docker run -d --net host srv.world/ubuntu-apache2

57c36cdc7633310526f549ea3040ecb388c201a9313436580ae83684ebe67058

root@dlp:~#
docker ps

CONTAINER ID   IMAGE                      COMMAND                  CREATED          STATUS          PORTS     NAMES
57c36cdc7633   srv.world/ubuntu-apache2   "/usr/sbin/apachectl…"   50 seconds ago   Up 49 seconds             sad_vaughan

# the port [apache2] service listens on container is used on Host network

root@dlp:~#
ss -napt

State     Recv-Q    Send-Q       Local Address:Port        Peer Address:Port
.....
.....
LISTEN    0         511                      *:80                     *:*        users:(("apache2",pid=2959,fd=4),("apache2",pid=2958,fd=4),("apache2",pid=2957,fd=4))

root@dlp:~#
curl localhost

index.html on Aapche2
Matched Content