Docker : Docker Network2023/06/22 |
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 11b02877db72 bridge bridge local 7e5fccb8cd81 host host local a58e9ea6ddae none null local e9c682aa11d5 root_default bridge local # display details of [bridge] root@dlp:~# docker network inspect bridge [ { "Name": "bridge", "Id": "11b02877db7276c9f302ec6211591ee67e674efda4f34637125191d58fe82d4f", "Created": "2023-06-22T01:56:04.398417557-05:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": null, "Config": [ { "Subnet": "172.17.0.0/16" } ] }, "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 debian /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.2root@dlp:~# docker commit $(docker ps -a|head -2|tail -1|awk '{print $1}') srv.world/debian-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 5a736efe2fc4abcc70fd27198bf48a384fb854ea0422bf111b6c28bfa719931croot@dlp:~# docker network ls NETWORK ID NAME DRIVER SCOPE 11b02877db72 bridge bridge local 7e5fccb8cd81 host host local 5a736efe2fc4 network01 bridge local a58e9ea6ddae none null local e9c682aa11d5 root_default bridge local # run a container with specifying [network01] root@dlp:~# docker run --net network01 srv.world/debian-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 752707cb6701 srv.world/debian-nginx "/usr/sbin/nginx -g …" 8 seconds ago Up 7 seconds 0.0.0.0:8081->80/tcp, :::8081->80/tcp friendly_williamsroot@dlp:~# docker exec 752707cb6701 /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 752707cb6701
docker exec 752707cb6701 /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 752707cb6701 root@dlp:~# docker exec 752707cb6701 /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 11b02877db72 bridge bridge local 7e5fccb8cd81 host host local 5a736efe2fc4 network01 bridge local a58e9ea6ddae none null local e9c682aa11d5 root_default bridge local # remove [network01] root@dlp:~# docker network rm network01 network01 |
[4] | To connect to Host network, set like follows. |
root@dlp:~# docker network ls NETWORK ID NAME DRIVER SCOPE 11b02877db72 bridge bridge local 7e5fccb8cd81 host host local a58e9ea6ddae none null local e9c682aa11d5 root_default bridge localroot@dlp:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE srv.world/debian-iproute latest 2b1c5b7c483d 5 minutes ago 151MB root_web latest d2928f1806c2 22 minutes ago 252MB srv.world/debian-apache2 latest 431c51e7819a 48 minutes ago 252MB srv.world/debian-nginx latest 5a202ab0ab76 53 minutes ago 153MB mariadb latest 99833200524a 6 days ago 403MB debian latest 49081a1edb0b 9 days ago 116MB # run a container with [host] network root@dlp:~# docker run -d --net host srv.world/debian-apache2 63b4e21a4647a1a9ec3ed9eb9baace67b61e3a49db9ca4e5314a7d3f9c4efa9droot@dlp:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 63b4e21a4647 srv.world/debian-apache2 "/usr/sbin/apachectl…" 10 seconds ago Up 9 seconds nice_sinoussi # 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=15703,fd=4),("apache2",pid=15702,fd=4),("apache2",pid=15701,fd=4))root@dlp:~# curl localhost index.html on Aapche2 |
Sponsored Link |
|