Docker : Docker Network2024/06/13 |
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 1badb20f0367 bridge bridge local e731ca77a91b host host local d718b9b6f9b5 none null local 402b85091089 root_default bridge local # display details of [bridge] root@dlp:~# docker network inspect bridge [ { "Name": "bridge", "Id": "1badb20f03672611887863141213b1943d28580b5ab49e1dbf2bfaf955bb7685", "Created": "2024-06-13T01:45:01.035387314Z", "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|head -2|tail -1|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 c9311e9a634a90119d43265ac56fed7fea301b3ca0ada5ed5b016d0358f272c5root@dlp:~# docker network ls NETWORK ID NAME DRIVER SCOPE 1badb20f0367 bridge bridge local e731ca77a91b host host local c9311e9a634a network01 bridge local d718b9b6f9b5 none null local 402b85091089 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 96fd8c3bd81d srv.world/ubuntu-nginx "/usr/sbin/nginx -g …" 8 seconds ago Up 8 seconds 0.0.0.0:8081->80/tcp, :::8081->80/tcp boring_coriroot@dlp:~# docker exec 96fd8c3bd81d /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 96fd8c3bd81d
docker exec 96fd8c3bd81d /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 96fd8c3bd81d root@dlp:~# docker exec 96fd8c3bd81d /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 1badb20f0367 bridge bridge local e731ca77a91b host host local c9311e9a634a network01 bridge local d718b9b6f9b5 none null local 402b85091089 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 1badb20f0367 bridge bridge local e731ca77a91b host host local d718b9b6f9b5 none null local 402b85091089 root_default bridge localroot@dlp:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE srv.world/ubuntu-iproute latest 6ca434303c4d 6 minutes ago 122MB srv.world/ubuntu-apache2 latest d987da01ec6c 3 hours ago 225MB srv.world/ubuntu-nginx latest f3f6605bb20c 3 hours ago 123MB ubuntu latest 17c0145030df 13 days ago 76.2MB dlp.srv.world:5000/ubuntu my-registry 17c0145030df 13 days ago 76.2MB # run a container with [host] network root@dlp:~# docker run -d --net host srv.world/ubuntu-apache2 b7833c3bffceb44b3623e83bc895901d5db37d12716396d7af5addd74f443da5root@dlp:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b7833c3bffce srv.world/ubuntu-apache2 "/usr/sbin/apachectl…" 14 seconds ago Up 14 seconds hardcore_mestorf # 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=23084,fd=4),("apache2",pid=23083,fd=4),("apache2",pid=23082,fd=4))root@dlp:~# curl localhost index.html on Aapche2 |
Sponsored Link |
|