Docker : Docker Network2026/05/08 |
|
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 89e5915a3230 bridge bridge local 45ee9acb0495 host host local c2567e635661 none null local e0d98b4bf1f2 root_default bridge local # display details of [bridge] root@dlp:~# docker network inspect bridge
[
{
"Name": "bridge",
"Id": "89e5915a323029a1068fcb1f13433d50f522e62bcaf641cde66569c3083edf5d",
"Created": "2026-05-08T00:53:17.608242818Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv4": true,
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"IPRange": "",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"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": {},
"Containers": {},
"Status": {
"IPAM": {
"Subnets": {
"172.17.0.0/16": {
"IPsInUse": 3,
"DynamicIPsAvailable": 65533
}
}
}
}
}
]
# [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 1de402f83798c69f6bb11a0d82f012955208f5002552340c978e10e69958e3d0root@dlp:~# docker network ls NETWORK ID NAME DRIVER SCOPE 89e5915a3230 bridge bridge local 45ee9acb0495 host host local 1de402f83798 network01 bridge local c2567e635661 none null local e0d98b4bf1f2 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 dec15b485d06 srv.world/ubuntu-nginx "/usr/sbin/nginx -g …" 1 second ago Up 1 second 0.0.0.0:8081->80/tcp, [::]:8081->80/tcp quirky_rideroot@dlp:~# docker exec dec15b485d06 /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 dec15b485d06
docker exec dec15b485d06 /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 dec15b485d06 root@dlp:~# docker exec dec15b485d06 /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 89e5915a3230 bridge bridge local 45ee9acb0495 host host local 1de402f83798 network01 bridge local c2567e635661 none null local e0d98b4bf1f2 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 89e5915a3230 bridge bridge local 45ee9acb0495 host host local c2567e635661 none null local e0d98b4bf1f2 root_default bridge localroot@dlp:~# docker images IMAGE ID DISK USAGE CONTENT SIZE EXTRA dlp.srv.world:5000/ubuntu:my-registry f3d28607ddd7 160MB 45.3MB U mariadb:latest e0236fc6386e 467MB 111MB root-web:latest 094ea18e9ca0 340MB 92.8MB srv.world/ubuntu-apache2:latest c6da801438b0 340MB 92.8MB U srv.world/ubuntu-iproute:latest 03427f8e5bf7 233MB 71.6MB U srv.world/ubuntu-nginx:latest 305dfeaadebc 227MB 69.9MB U ubuntu:latest f3d28607ddd7 160MB 45.3MB U # run a container with [host] network root@dlp:~# docker run -d --net host srv.world/ubuntu-apache2 feb46d9a3d361a57ccfd61f199d5af3a67e81ad9d3ac6fb51593a4532c699fedroot@dlp:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES feb46d9a3d36 srv.world/ubuntu-apache2 "/usr/sbin/apachectl…" 11 seconds ago Up 10 seconds friendly_tharp # 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=24523,fd=4),("apache2",pid=24522,fd=4),("apache2",pid=24521,fd=4))
root@dlp:~# curl localhost index.html on Aapche2 |
| Sponsored Link |
|
|