Docker : Docker Network2023/04/27 |
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 d5180938b0a8 bridge bridge local 6760b95b1dd1 host host local ff57b05d9bdc none null local 9d6424d9014d root_default bridge local # display details of [bridge] root@dlp:~# docker network inspect bridge [ { "Name": "bridge", "Id": "d5180938b0a88bd697e26e2e0456688aec577d6670cd43fa59a0b45d0b5063f3", "Created": "2023-04-27T01:47:23.332133453Z", "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.2root@dlp:~# docker commit $(docker ps -a | sed -n 2p | awk '{print $1}') 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 e3328bd35135bd534a5d2bafafd7455d666a8d389224b881327c6a3520b17d60root@dlp:~# docker network ls NETWORK ID NAME DRIVER SCOPE d5180938b0a8 bridge bridge local 6760b95b1dd1 host host local e3328bd35135 network01 bridge local ff57b05d9bdc none null local 9d6424d9014d 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 7e737a6fd180 srv.world/ubuntu-nginx "/usr/sbin/nginx -g …" 8 seconds ago Up 8 seconds 0.0.0.0:8081->80/tcp, :::8081->80/tcp agitated_pascalroot@dlp:~# docker exec 7e737a6fd180 /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 7e737a6fd180
docker exec 7e737a6fd180 /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 7e737a6fd180 root@dlp:~# docker exec 7e737a6fd180 /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 d5180938b0a8 bridge bridge local 6760b95b1dd1 host host local e3328bd35135 network01 bridge local ff57b05d9bdc none null local 9d6424d9014d root_default bridge local # remove [network01] root@dlp:~# docker network rm network01 network01 |
[4] | To connect to Host network, not bridge, set like follows. |
root@dlp:~# docker network ls NETWORK ID NAME DRIVER SCOPE d5180938b0a8 bridge bridge local 6760b95b1dd1 host host local ff57b05d9bdc none null local 9d6424d9014d root_default bridge localroot@dlp:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE srv.world/ubuntu-iproute latest a52d4a7663a0 5 minutes ago 126MB root_web latest 75ab1e6bff3e 37 minutes ago 235MB srv.world/ubuntu-apache2 latest 886e2f7307a1 58 minutes ago 235MB srv.world/ubuntu-nginx latest df0287c5017d About an hour ago 177MB mariadb latest 4a632f970181 5 weeks ago 401MB dlp.srv.world:5000/ubuntu my-registry 08d22c0ceb15 7 weeks ago 77.8MB ubuntu latest 08d22c0ceb15 7 weeks ago 77.8MB # run a container with [host] network root@dlp:~# docker run -d --net host srv.world/ubuntu-apache2 a0e60a616d6ad4ab9a9f471cc350d2d6d817984abe780de8d8888083e1fb961broot@dlp:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a0e60a616d6a srv.world/ubuntu-apache2 "/usr/sbin/apachectl…" 10 seconds ago Up 9 seconds frosty_chatterjee # 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=14363,fd=4),("apache2",pid=14362,fd=4),("apache2",pid=14361,fd=4))root@dlp:~# curl localhost index.html on Aapche2 |
Sponsored Link |