Docker Network Commands
Source: Dev.to
Docker network Command Overview
The docker network command is used to manage Docker networks. It lets you create, inspect, list, connect, disconnect, and remove networks.
1. List Networks
docker network ls
Sample output
| NETWORK ID | NAME | DRIVER | SCOPE |
|---|---|---|---|
| abcdef123456 | bridge | bridge | local |
| ghijkl789012 | host | host | local |
| mnopqr345678 | my-network | bridge | local |
| stuvwx901234 | my-overlay-net | overlay | swarm |
Explanation
| Column | Description |
|---|---|
| NETWORK ID | Unique identifier for each network |
| NAME | Network name |
| DRIVER | Driver type (e.g., bridge, overlay, host) |
| SCOPE | local – only on the current host; global – across a swarm |
2. Inspect a Network
docker network inspect <name-or-id>
Replace <name-or-id> with the actual name or ID.
Example
docker network inspect my-network
Sample JSON output (truncated for brevity)
[
{
"Name": "my-network",
"Id": "abcdef1234567890",
"Created": "2022-01-01T12:00:00Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Containers": {
"container1": {
"Name": "container1",
"EndpointID": "xyz123456789",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"container2": {
"Name": "container2",
"EndpointID": "abc987654321",
"MacAddress": "02:42:ac:11:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
The inspection provides details such as creation time, driver, IPAM configuration, connected containers, and more—useful for troubleshooting and understanding network settings.
3. Create a Network
Simple creation (default bridge driver)
docker network create my-network
Custom creation with options
docker network create \
--driver bridge \
--subnet 172.18.0.0/16 \
--gateway 172.18.0.1 \
--ip-range 172.18.0.0/24 \
my-custom-network
| Option | Meaning |
|---|---|
--driver bridge | Use the bridge driver |
--subnet 172.18.0.0/16 | Define the network subnet |
--gateway 172.18.0.1 | Set the gateway IP |
--ip-range 172.18.0.0/24 | Limit the assignable IP range for containers |
my-custom-network | Name of the new network |
Verify creation with docker network ls.
4. Connect a Container to a Network
docker network connect my-network my-container
my-network– target network namemy-container– container name or ID
The container can now communicate with other containers on my-network using their names or IP addresses.
5. Disconnect a Container from a Network
docker network disconnect my-network my-container
my-network– network to disconnect frommy-container– container name or ID
After disconnecting, the container loses communication with peers on that network (though it may remain attached to other networks).
6. Remove a Network
docker network rm <network>
Replace <network> with the network you wish to delete.
Example
docker network rm my-network
Note: A network cannot be removed while containers are still attached to it. Disconnect or stop those containers first.
Docker Network Commands Cheat‑Sheet
Below is a cleaned‑up reference for the most common Docker network commands, their usage, and examples.
1. Remove a Network
docker network rm <network>
Example
docker network rm my-network
my-network is the name (or ID) of the Docker network you want to delete. Docker removes the specified network; containers exclusively attached to it are disconnected, while those attached to multiple networks stay connected to the others.
2. Prune Unused Networks
docker network prune
Running the command without options prompts for confirmation:
WARNING! This will remove all unused networks.
Are you sure you want to continue? [y/N] y
Skip Confirmation
docker network prune -f # or --force
Only unused networks (those not attached to any container) are removed. Networks still in use are left untouched.
3. Create a Custom IPAM Configuration
docker network create-ipam --subnet=<subnet> <network-name>
Example
docker network create-ipam --subnet=192.168.1.0/24 my-custom-network
--subnet=192.168.1.0/24– defines the IP address range for the network.my-custom-network– name of the Docker network that will use this IPAM configuration.
If you omit custom IPAM, Docker applies its default settings.
4. Connect a Container Using a Custom IPAM
docker network connect-ipam --ip=<ip> <network-name> <container>
Example
docker network connect-ipam --ip=192.168.1.10 my-custom-network my-container
--ip=192.168.1.10– static IP for the container on that network.my-custom-network– the network with the custom IPAM configuration.my-container– the target container.
5. Disconnect a Container from a Custom IPAM Network
docker network disconnect-ipam <network-name> <container>
Example
docker network disconnect-ipam my-custom-network my-container
Important:
disconnect-ipamis only for networks that use a custom IPAM configuration. For default‑IPAM networks, use the regulardocker network disconnectcommand.
6. Example: Creating a Bridge Network and Testing Connectivity
# List running containers (none at this point)
docker container ls
#> CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# List existing networks
docker network ls
#> NETWORK ID NAME DRIVER SCOPE
#> 6c51373f78ad bridge bridge local
#> 3a33f83c3664 host host local
#> e4ebd601732e none null local
# Run two Ubuntu containers in detached mode
docker container run -itd ubuntu:14.04 bash
#> 7b1af2ee48e43f8018c4324bbcb9f52a27f741bd7a0437ddd0f6766bd7ca6b10
docker container run -itd ubuntu:14.04 bash
#> c3ce5dbe5d859705f139e811bc11367d02bf0969492e9d515a3cc6cc636ddfbb
# Verify they are running
docker container ls
#> CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
#> c3ce5dbe5d85 ubuntu:14.04 "bash" 8 seconds ago Up 7 seconds unruffled_sinoussi
#> 7b1af2ee48e4 ubuntu:14.04 "bash" 10 seconds ago Up 9 seconds trusting_joliot
# Try to ping one container from the other (fails because they are on the default bridge)
docker container exec -it 7b1af2ee48e4 bash
#> root@7b1af2ee48e4:/# ping c3ce5dbe5d85
#> ping: unknown host c3ce5dbe5d85
# Create a custom bridge network (DNS is enabled by default on custom bridges)
docker network create test
#> b1e05c1afdb2f901e
Now you can attach the containers to the test network (using docker network connect or by launching them with --network test) and they will be able to resolve each other by name.
Quick Reference Table
| Command | Purpose | Example |
|---|---|---|
docker network rm | Delete a specific network | docker network rm my-network |
docker network prune | Remove all unused networks | docker network prune -f |
docker network create-ipam | Create a network with custom IPAM | docker network create-ipam --subnet=10.0.0.0/24 my-net |
docker network connect-ipam | Attach a container with a static IP | docker network connect-ipam --ip=10.0.0.5 my-net my-container |
docker network disconnect-ipam | Detach a container from a custom‑IPAM network | docker network disconnect-ipam my-net my-container |
docker network create | Create a regular (bridge) network (DNS enabled) | docker network create test |
Additional Commands & Examples
81a66a52d64a9dcdca9c5cab98433cdaed2faa83c5b3e6b
List containers (none running)
localhost:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Create a container with the ubuntu image on the test network
localhost:~$ docker container run -itd --network=test ubuntu:14.04 bash
c7b07b61bb20cdbb6e1b54a165aed0f8907d95d563fdd7a60940d004694c4557
List the containers again
localhost:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7b07b61bb20 ubuntu "bash" About a minute ago Up About a minute
Inspect the container (excerpt)
{
"Networks": {
"host": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "b1e05c1afdb2f901e81a66a52d64a9dcdca9c5cab98433cdaed2faa83c5b3e6b",
"EndpointID": "b860ca4fdda3e0732367949cb94fd2eded08a4f2e46715a6c125b1bf336c102f",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
Delete a network
localhost:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
6c51373f78ac bridge bridge local
3a33f83c3663 host host local
e4ebd601732c none null local
348f7295d3ca test bridge local
localhost:~$ docker network rm test
test
localhost:~$
📝 Liked this blog?
Buy me a coffee ☕
What is None network driver
Meghasharmaa