Docker : Docker Network2021/08/30 |
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 5c55db07cd7d bridge bridge local 44eb6a4d6607 host host local bdc9e1352e8c none null local # display details of [bridge] root@dlp:~# docker network inspect bridge [ { "Name": "bridge", "Id": "5c55db07cd7d2581c10e61a37bb5955de4665d7ef20a11e0a3bb93e804e828a2", "Created": "2021-08-29T19:30:35.142430368-05: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 debian /bin/bash -c "apt-get update; apt-get -y install iproute2; 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 0a5dcda0692f75f9c2bdad8e92898b9e9fa08ef6a1116d121c28d03f5ac8c03croot@dlp:~# docker network ls NETWORK ID NAME DRIVER SCOPE 5c55db07cd7d bridge bridge local 44eb6a4d6607 host host local 0a5dcda0692f network01 bridge local bdc9e1352e8c none null local # run a container with specifying [network01] root@dlp:~# docker run --net network01 debian /bin/bash -c "apt-get update; apt-get -y install iproute2; 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 46c0b7b622d3 srv.world/debian-apache2 "/usr/sbin/apachectl…" 14 seconds ago Up 14 seconds 0.0.0.0:8081->80/tcp hardcore_rhodesroot@dlp:~# docker exec 46c0b7b622d3 /bin/bash -c "apt-get update; apt-get -y install iproute2; 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 46c0b7b622d3
docker exec 46c0b7b622d3 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 46c0b7b622d3 root@dlp:~# docker exec 46c0b7b622d3 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 5c55db07cd7d bridge bridge local 44eb6a4d6607 host host local 0a5dcda0692f network01 bridge local bdc9e1352e8c none null 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
|
[4] | To connect to Host network, not bridge, set like follows. |
root@dlp:~# docker network ls NETWORK ID NAME DRIVER SCOPE 5c55db07cd7d bridge bridge local 44eb6a4d6607 host host local bdc9e1352e8c none null localroot@dlp:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE srv.world/debian-apache2 latest 35b4db75bde1 47 minutes ago 252MB srv.world/debian-nginx latest e07733601f30 51 minutes ago 210MB dlp.srv.world:5000/debian my-registry fe3c5de03486 13 days ago 124MB debian latest fe3c5de03486 13 days ago 124MB # run a container with [host] network root@dlp:~# docker run -d --net host srv.world/debian-apache2 647ca2a6709f4c41f34f8c945ab314730ccecd8c9ffe721f59e89b2ccbc03a78root@dlp:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 647ca2a6709f srv.world/debian-apache2 "/usr/sbin/apachectl…" 6 seconds ago Up 5 seconds suspicious_goldstine # 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=15134,fd=4),("apache2",pid=15133,fd=4),("apache2",pid=15132,fd=4))root@dlp:~# curl localhost index.html on Aapche2 |
Sponsored Link |
|