Docker : Docker Network2021/05/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 eb0bb379e2ec bridge bridge local 6950c914e786 host host local b47801ff9a1b none null local c4eb2de2e30c root_default bridge local # display details of [bridge] root@dlp:~# docker network inspect bridge [ { "Name": "bridge", "Id": "eb0bb379e2ecc8697a8083979dddc8bdfe857b776151a0a2e3d456c2e838327d", "Created": "2021-05-12T06:30:08.739609048Z", "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.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 82bcd9fe7673350b410959d8316d603eb5ddcff67a0de0079b3a0250923ba8adroot@dlp:~# docker network ls NETWORK ID NAME DRIVER SCOPE eb0bb379e2ec bridge bridge local 6950c914e786 host host local 82bcd9fe7673 network01 bridge local b47801ff9a1b none null local c4eb2de2e30c root_default bridge local # run a container with specifying [network01] root@dlp:~# docker run --net network01 ubuntu /bin/bash -c "apt-get update; apt-get -y install iproute2; /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 de3543f828c0 srv.world/ubuntu-nginx "/usr/sbin/nginx -g …" 10 seconds ago Up 8 seconds 0.0.0.0:8081->80/tcp exciting_proskuriakovaroot@dlp:~# docker exec de3543f828c0 /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 de3543f828c0
docker exec de3543f828c0 /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 de3543f828c0 root@dlp:~# docker exec de3543f828c0 /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 eb0bb379e2ec bridge bridge local 6950c914e786 host host local 82bcd9fe7673 network01 bridge local b47801ff9a1b none null local c4eb2de2e30c 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 eb0bb379e2ec bridge bridge local 6950c914e786 host host local b47801ff9a1b none null localroot@dlp:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE srv.world/ubuntu-apache2 latest 84bcc150feb9 5 hours ago 216MB srv.world/ubuntu-nginx latest 8f1fbe417eb2 5 hours ago 160MB mariadb latest 992bce5ed710 2 weeks ago 401MB ubuntu latest 7e0aa2d69a15 2 weeks ago 72.7MB nginx latest 62d49f9bab67 4 weeks ago 133MB dlp.srv.world:5000/nginx my-registry 62d49f9bab67 4 weeks ago 133MB # run a container with [host] network root@dlp:~# docker run -d --net host srv.world/ubuntu-apache2 6b8e52abfbb4b5a36146d1cf15f345a8e1e7ddf31966a0a420647c7a3828290froot@dlp:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6b8e52abfbb4 srv.world/ubuntu-apache2 "/usr/sbin/apachectl…" 6 seconds ago Up 5 seconds vigilant_curie # 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=20871,fd=4),("apache2",pid=20870,fd=4),("apache2",pid=20869,fd=4))root@dlp:~# curl localhost index.html on Aapche2 |
Sponsored Link |
|