This error occurs when you try to create a Docker network that already exists. The fix is simple: either use the existing network, remove it first, or check if it exists before creating.
The "network with name already exists" error occurs when Docker tries to create a network but finds that a network with the same name is already registered in the Docker daemon. Every Docker network must have a unique name within the Docker host. This commonly happens in several scenarios: - Running `docker network create` multiple times with the same network name - Docker Compose tries to create its default network but it already exists from a previous run - Scripts or CI pipelines that create networks without checking if they already exist - A previous container or compose stack wasn't properly cleaned up The error is straightforward to resolve - you just need to either use the existing network, remove it, or add a check before creating.
First, verify the network exists and inspect its details:
docker network ls | grep mynetworkTo see full details of the existing network:
docker network inspect mynetworkThis shows which containers are connected to it and its configuration.
If the network already exists and has the configuration you need, simply use it. In docker-compose.yml, you can reference an existing external network:
networks:
mynetwork:
external: trueFor docker run, just connect to the existing network:
docker run --network mynetwork myimageIf you want to recreate the network fresh, remove the existing one first:
docker network rm mynetworkIf containers are still connected to the network, you'll need to disconnect or stop them first:
# See what's connected
docker network inspect mynetwork --format '{{range .Containers}}{{.Name}} {{end}}'
# Disconnect a container
docker network disconnect mynetwork container_name
# Or stop all connected containers
docker stop $(docker network inspect mynetwork -f '{{range .Containers}}{{.Name}} {{end}}')
# Then remove the network
docker network rm mynetworkThe best approach for scripts and automation is to check before creating. Use this one-liner:
docker network inspect mynetwork >/dev/null 2>&1 || docker network create mynetworkThis inspects the network (silently) and only creates it if the inspect command fails.
For more robust scripts, use a filter with exact matching:
if [ -z "$(docker network ls --filter name=^mynetwork$ --format '{{.Name}}')" ]; then
docker network create mynetwork
fiThe ^mynetwork$ regex ensures exact matching (not partial matches like "mynetwork2").
If you have multiple orphaned networks causing issues, clean them all up:
docker network pruneThis removes all networks not used by at least one container. Add -f to skip the confirmation prompt:
docker network prune -fWarning: This removes ALL unused networks, so make sure you don't need them.
Docker Compose creates networks named <project>_default or <project>_<network>. If you get conflicts:
Option 1: Bring down the old project properly:
docker-compose downOption 2: Use a different project name:
docker-compose -p newproject upOption 3: Force recreate everything:
docker-compose up --force-recreateHandling in CI/CD pipelines: Always use the "create if not exists" pattern in CI/CD to make pipelines idempotent:
docker network inspect mynetwork >/dev/null 2>&1 || docker network create --driver bridge mynetworkReturning the network ID: If your script needs the network ID regardless of whether it was just created:
docker network inspect mynetwork --format '{{.Id}}' 2>/dev/null || docker network create mynetworkDocker Compose external networks: When multiple Compose projects need to share a network, create it externally once:
docker network create shared_networkThen in all docker-compose.yml files:
networks:
shared_network:
external: trueForce disconnect stuck containers: Sometimes a container endpoint gets stuck in a network after a crash. Force disconnect with:
docker network disconnect --force mynetwork container_nameRestarting Docker daemon: As a last resort for stuck networks that won't delete, restart the Docker daemon:
sudo systemctl restart dockerThis clears any corrupted network state. Note: this will stop all running containers.
Windows/Mac with Docker Desktop: On Docker Desktop, you can also reset Docker to factory defaults from the GUI if network issues persist. Go to Settings > Reset > Reset to factory defaults. This removes all containers, images, and networks.
dockerfile parse error line 5: unknown instruction: RRUN
How to fix 'unknown instruction' Dockerfile parse error in Docker
Error response from daemon: manifest for nginx:nonexistent not found: manifest unknown: manifest unknown
How to fix 'manifest for image:tag not found' in Docker
Error response from daemon: invalid reference format: repository name must be lowercase
How to fix 'repository name must be lowercase' in Docker
Error response from daemon: No such image
How to fix 'No such image' in Docker
Error response from daemon: Container is not running
How to fix 'Container is not running' when using docker exec