How to Remove Network in Docker: Simple Commands Explained
To remove a network in Docker, use the command
docker network rm <network_name>. Make sure no containers are connected to the network before removing it, or the command will fail.Syntax
The basic syntax to remove a Docker network is:
docker network rm <network_name_or_id>: Removes the specified network by its name or ID.
You must specify the exact network name or ID. The network must not be in use by any container.
bash
docker network rm my-network
Example
This example shows how to create a network, list networks, and then remove the created network.
bash
docker network create my-network docker network ls docker network rm my-network docker network ls
Output
a1b2c3d4e5f6
NETWORK ID NAME DRIVER SCOPE
...
my-network bridge local
...
NETWORK ID NAME DRIVER SCOPE
...
Common Pitfalls
Trying to remove a network that is still used by running containers will fail with an error.
Example error: Error response from daemon: network my-network has active endpoints
To fix this, stop or disconnect containers from the network before removing it.
bash
docker network rm my-network # Error: network my-network has active endpoints docker network disconnect my-network container_id # Then remove network docker network rm my-network
Output
Error response from daemon: network my-network has active endpoints
network my-network
Quick Reference
| Command | Description |
|---|---|
| docker network ls | List all Docker networks |
| docker network create | Create a new Docker network |
| docker network rm | Remove a Docker network by name or ID |
| docker network disconnect | Disconnect a container from a network |
Key Takeaways
Use
docker network rm <network_name> to remove a Docker network.Ensure no containers are connected to the network before removal.
If removal fails, disconnect or stop containers using the network first.
List networks with
docker network ls to confirm network names.Removing default networks like 'bridge' is not allowed.