CentOS Stream 8
Sponsored Link

Docker : Docker Network
2021/04/06
 
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
cee9402908b9   bridge         bridge    local
e7b1b2a15508   host           host      local
3696ca2c53b8   none           null      local
df0619e46838   root_default   bridge    local

# display details of [bridge]

[root@dlp ~]#
docker network inspect bridge

[
    {
        "Name": "bridge",
        "Id": "cee9402908b9e314c3b37adba7898b54a8fe31b4a16d2585de74d5e38f2425c6",
        "Created": "2021-04-06T00:53:09.912662064-06:00",
        "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 centos /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
[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

bc813cd90c60e04035294d72d7c0e9ebd8d4cbec8cdaab019ca2f1c2e1b6c2a9

[root@dlp ~]#
docker network ls

NETWORK ID     NAME           DRIVER    SCOPE
cee9402908b9   bridge         bridge    local
e7b1b2a15508   host           host      local
bc813cd90c60   network01      bridge    local
3696ca2c53b8   none           null      local
df0619e46838   root_default   bridge    local

# run a container with specifying [network01]

[root@dlp ~]#
docker run --net network01 centos /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
f9465c672cdf   srv.world/centos-httpd   "/usr/sbin/httpd -D …"   10 seconds ago   Up 9 seconds   0.0.0.0:8081->80/tcp   silly_lamarr

[root@dlp ~]#
docker exec f9465c672cdf 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 f9465c672cdf
[root@dlp ~]#
docker exec f9465c672cdf 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 f9465c672cdf

[root@dlp ~]#
docker exec f9465c672cdf 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
cee9402908b9   bridge         bridge    local
e7b1b2a15508   host           host      local
bc813cd90c60   network01      bridge    local
3696ca2c53b8   none           null      local
df0619e46838   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, not bridge, set like follows.
[root@dlp ~]#
docker network ls

NETWORK ID     NAME      DRIVER    SCOPE
cee9402908b9   bridge    bridge    local
e7b1b2a15508   host      host      local
3696ca2c53b8   none      null      local

[root@dlp ~]#
docker images

REPOSITORY               TAG       IMAGE ID       CREATED             SIZE
srv.world/centos-httpd   latest    83fe1ffbabdb   About an hour ago   250MB
srv.world/centos-nginx   latest    ae7ab6688f5e   About an hour ago   289MB
mariadb                  latest    e76a4b2ed1b4   3 days ago          401MB
registry                 2         ee34aa9d8ab2   4 days ago          26.2MB
centos                   latest    300e315adb2f   3 months ago        209MB

# run a container with [host] network

[root@dlp ~]#
docker run -d --net host srv.world/centos-httpd

93722bef91e7db60219fbac1e551e987b33b863bb2157ab9676df995dbaff684

[root@dlp ~]#
docker ps

CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS          PORTS                  NAMES
93722bef91e7   srv.world/centos-httpd   "/usr/sbin/httpd -D …"   6 seconds ago    Up 5 seconds                           bold_pare

# the port [httpd] 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      128                *:80              *:*    users:(("httpd",pid=12673,fd=4),("httpd",pid=12669,fd=4),("httpd",pid=12668,fd=4),("httpd",pid=12651,fd=4))

[root@dlp ~]#
curl localhost

Index.html on Aapche httpd
Matched Content