Docker : Docker Network2022/07/29 |
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 c09007ca779f bridge bridge local 4987b54add2b host host local 453925c235ff none null local f172cf624e51 root_default bridge local # display details of [bridge] [root@dlp ~]# docker network inspect bridge [ { "Name": "bridge", "Id": "c09007ca779f967dd528a90df7d941821bb7dcd74c23f7ad02cfa81b781b13b8", "Created": "2022-07-29T14:29:26.655245499+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 quay.io/centos/centos:stream9 /bin/bash -c "dnf -y install iproute; /usr/sbin/ip route" -name centos-iproute 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 3d3034b1a9de quay.io/centos/centos:stream9 "/bin/bash -c 'dnf -…" 44 seconds ago Exited (0) 2 seconds ago naughty_varahamihira[root@dlp ~]# docker commit 3d3034b1a9de srv.world/centos-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 787d8ac37e8e2e6f3d289b3babd61e96146dc75c24367484ae9acb7c29a510ce[root@dlp ~]# docker network ls NETWORK ID NAME DRIVER SCOPE c09007ca779f bridge bridge local 4987b54add2b host host local 787d8ac37e8e network01 bridge local 453925c235ff none null local f172cf624e51 root_default bridge local # run a container with specifying [network01] [root@dlp ~]# docker run --net network01 srv.world/centos-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 acfcd97f01d2 srv.world/centos-iproute "/bin/bash" 5 seconds ago Up 4 seconds great_bartik[root@dlp ~]# docker exec acfcd97f01d2 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 acfcd97f01d2
docker exec acfcd97f01d2 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 acfcd97f01d2 [root@dlp ~]# docker exec acfcd97f01d2 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 c09007ca779f bridge bridge local 4987b54add2b host host local 787d8ac37e8e network01 bridge local 453925c235ff none null local f172cf624e51 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 c09007ca779f bridge bridge local 4987b54add2b host host local 453925c235ff none null local[root@dlp ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE srv.world/centos-iproute latest 9c37287615b8 12 minutes ago 212MB srv.world/centos-httpd latest db5fee1dd895 3 hours ago 257MB srv.world/centos-nginx latest 7fd90c511873 3 hours ago 251MB quay.io/centos/centos stream9 61674c24ebbf 33 hours ago 152MB registry 2 d1fe2eaf6101 10 days ago 24.1MB # run a container with [host] network [root@dlp ~]# docker run -d --net host srv.world/centos-httpd a59c5732539346f970267542e9354e89994907e7782cf65974406683f5f28c7b[root@dlp ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a59c57325393 srv.world/centos-httpd "/usr/sbin/httpd -D …" 9 seconds ago Up 8 seconds magical_meitner # 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 511 *:80 *:* users:(("httpd",pid=14449,fd=4),("httpd",pid=14448,fd=4),("httpd",pid=14447,fd=4),("httpd",pid=14433,fd=4))[root@dlp ~]# curl localhost Index.html on Aapche httpd |
Sponsored Link |
|