The docker network create command failed because the network name you specified, or a network with that name already exists on your Docker host.
Here’s how to nail down the cause and fix it:
1. The Network Actually Exists (Most Common)
You’re trying to create a network that’s already there, maybe from a previous failed docker-compose up or a manual docker network create you forgot about.
- Diagnosis: List all networks and look for your name:
docker network ls - Fix:
- Option A (Remove it): If you don’t need the old network, remove it.
This frees up the network name, allowingdocker network rm <network_name>docker network createto succeed. - Option B (Use the existing one): If the network should exist and you want to use it, stop trying to create it. Your application (e.g.,
docker-compose up) should be able to attach to it. If it’s a manual creation, you’ll need to inspect the existing network to ensure it’s configured as you expect.
This command shows you all the details about the existing network, including its driver, subnet, gateway, and connected containers, allowing you to verify its configuration.docker network inspect <network_name>
- Option A (Remove it): If you don’t need the old network, remove it.
2. Docker Daemon State Inconsistency
Sometimes, Docker’s internal state gets a little wonky. It might think a network exists when it doesn’t, or vice-versa, leading to this error even after you’ve checked docker network ls and confirmed it’s not there.
- Diagnosis: Check Docker daemon logs for any related errors around the time you tried to create the network. The location varies by OS, but often found in
/var/log/docker.logor viajournalctl -u docker.service. - Fix: Restart the Docker daemon. This often clears up internal inconsistencies.
- Linux (systemd):
sudo systemctl restart docker - macOS/Windows: Use the Docker Desktop GUI to restart the engine. This forces Docker to re-initialize its state, resolving phantom network entries.
- Linux (systemd):
3. Docker Compose Leftover Network
If you’re using docker-compose, it manages networks for your services. A previous docker-compose up might have created a network, and if the docker-compose.yml file has changed or the compose project was interrupted, Docker might still see that network.
- Diagnosis: Look for a network named
<project_name>_<network_name>in the output ofdocker network ls. The<project_name>is usually the directory name where yourdocker-compose.ymlresides. - Fix:
- Option A (Clean up compose project):
docker-compose down -v docker-compose up -ddocker-compose down -vstops and removes all containers, networks, and volumes associated with the compose project. This ensures a clean slate beforedocker-compose up -drecreates everything. - Option B (Manual removal if
docker-compose downfails): Ifdocker-compose downdoesn’t clean up, you might need to manually remove the network identified in the diagnosis step.
Then trydocker network rm <project_name>_<network_name>docker-compose up -dagain.
- Option A (Clean up compose project):
4. Conflicting Network Driver or Configuration
You might be trying to create a network with specific options (like a custom subnet or gateway) that conflict with an existing network or Docker’s network configuration. This is less common for a simple "network already exists" error but can manifest if Docker tries to create a network similar to an existing one and gets confused.
- Diagnosis: Compare the
docker network inspectoutput of any existing networks with similar names or configurations to thedocker network createcommand you’re attempting. Pay close attention toIPAM(IP Address Management) configurations. - Fix: Adjust your
docker network createcommand to avoid conflicts.- If you’re specifying a subnet, ensure it doesn’t overlap with existing Docker networks or your host’s network. For example, if you have a bridge network using
172.17.0.0/16, don’t try to create another bridge network using the same subnet. - Remove the conflicting IPAM configuration if you don’t strictly need it.
This command creates a new bridge network named# Example: Avoid creating a network with a subnet already in use docker network create --driver bridge --subnet 172.20.0.0/16 my_custom_networkmy_custom_networkusing the specified IPAM configuration, assuming172.20.0.0/16is not already in use by another Docker network. - If you’re specifying a subnet, ensure it doesn’t overlap with existing Docker networks or your host’s network. For example, if you have a bridge network using
5. Corrupted Docker Network Configuration Files
In rare cases, the actual configuration files on disk that represent Docker’s networks can become corrupted.
- Diagnosis: This is hard to diagnose directly without deep Docker internals knowledge. Look for filesystem errors or unusual file sizes in Docker’s data directories (e.g.,
/var/lib/docker/networkon Linux). - Fix: A more drastic measure is to prune all unused networks and then restart Docker.
docker network prune sudo systemctl restart dockerdocker network pruneremoves all networks not used by at least one container. This is a more aggressive cleanup thandocker network rmand can resolve issues stemming from corrupted internal network state if restarting the daemon alone didn’t work.
6. Docker Swarm Interference
If you are running Docker Swarm, networks can behave differently and might be managed by Swarm’s internal state rather than direct docker network commands. A network might be marked as "global" or "ingress" and appear to exist when you try to create it locally.
- Diagnosis: Check if your Docker daemon is part of a Swarm.
If Swarm is active, inspect networks with Swarm context in mind.docker info | grep Swarm - Fix:
- If it’s a Swarm network you’re trying to create locally, you likely need to create it as a Swarm network.
This command creates a distributed overlay network compatible with Swarm.docker network create --driver overlay my_overlay_network - If you are not intending to use Swarm and it’s active, you might need to leave the Swarm.
This command forces the current node to leave the Swarm cluster.docker swarm leave --force
- If it’s a Swarm network you’re trying to create locally, you likely need to create it as a Swarm network.
After fixing, you’ll likely encounter a "port is already allocated" error if you’ve tried to map a host port that’s already in use by another container.