CentOS 8
Sponsored Link

Docker : Docker Network2020/12/14

 
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
9f458673f67c   bridge    bridge    local
468f5f76df4c   host      host      local
c8d163c6fc77   none      null      local

# display details of [bridge]

[root@dlp ~]#
docker network inspect bridge

[
    {
        "Name": "bridge",
        "Id": "9f458673f67c9e81113a90407d6760df72780b2808d4e405cb09324fd49014d8",
        "Created": "2020-12-14T09:00:26.264698348+09: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

9ff8a00253f3bd6f7a851aedbee16b5671817e926a4b428e1f3b515b53fdcac8

[root@dlp ~]#
docker network ls

NETWORK ID     NAME        DRIVER    SCOPE
9f458673f67c   bridge      bridge    local
468f5f76df4c   host        host      local
9ff8a00253f3   network01   bridge    local
c8d163c6fc77   none        null      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
d84f22149b12   srv.world/centos-httpd   "/usr/sbin/httpd -D …"   4 minutes ago   Up 4 minutes   80/tcp    angry_lumiere

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

[root@dlp ~]#
docker exec d84f22149b12 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
9f458673f67c   bridge         bridge    local
468f5f76df4c   host           host      local
9ff8a00253f3   network01      bridge    local
c8d163c6fc77   none           null      local
26f18c5ab461   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
9f458673f67c   bridge    bridge    local
468f5f76df4c   host      host      local
c8d163c6fc77   none      null      local

[root@dlp ~]#
docker images

REPOSITORY               TAG       IMAGE ID       CREATED        SIZE
srv.world/centos-httpd   latest    fed1671fb260   3 days ago     250MB
centos                   latest    300e315adb2f   6 days ago     209MB
registry                 2         2d4f4b5309b1   5 months ago   26.2MB

# run a container with [host] network

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

28c45aa5c77276cf757d0d3444613a052ef4c4e761d8b55ee16af8506afb096d

[root@dlp ~]#
docker ps

CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS        PORTS     NAMES
28c45aa5c772   srv.world/centos-httpd   "/usr/sbin/httpd -D …"   2 seconds ago   Up 1 second             admiring_mestorf

# 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=4477,fd=4),("httpd",pid=4474,fd=4),("httpd",pid=4473,fd=4),("httpd",pid=4454,fd=4))

[root@dlp ~]#
curl localhost

Index.html on Aapche httpd
Matched Content