Docker Network Exists but Was Not Created for Project

Last updated 12th November, 2023

What Causes the Broken Network?

Occasionally things go wrong in Docker. That's just been my experience 😅. I've seen this error occur in a couple of scenarios:

  • Manually creating/updating networks in docker that Docker Compose was using.
  • Updating my Docker Compose version and restarting services.

I've encountered this issue in both of those cases. So how do we fix it?

Identify the Broken Network

The error message you received should have the broken network in it.

To check this, run the command:

docker network ls

The output should look something like this:

Terminal output screenshot after running the network list command

Remove the Broken Network

Once you've found the offending network, stop all containers that reference that network.

docker network rm network_in_use
# Error response from daemon: error while removing network:
# network network_in_use id
# 1e6f42e02093e63001d6f95013e4fa4401bb8177bb71a4a96d1992e4a1716d57 has active endpoints

The Docker Network Still Has Active Endpoints

If you get an error like "Docker Network Rm Has Active Endpoints", it means that you still have containers running that reference that network.

You'll want to shut down all containers that use the network, or docker won't let you remove it.

Shutdown the containers on the network with docker-compose, or docker commands:

cd ~/project_directory
docker-compose down

# Or using docker...

docker stop container_ids

Removing Missing Services with Remove Orphans

In the event that you have removed a service from a docker-compose.yaml file, running docker-compose down will not remove that missing service - preventing you from removing the problematic network.

The --remove-orphans flag on the docker-compose down command will remove any dangling services no longer included in the compose file. Docker Compose documentation.

docker-compose down --remove-orphans

We can now remove the network by running:

docker network rm [YOUR_NETWORK_NAME]

# This will print out the name of your removed network
# e.g.

docker network rm example_network

# example_network

Recreating The Docker Network

To recreate your network, simply run your containers again. This will allow Docker Compose to naturally create the network - and should resolve your issue!

docker-compose up -d

Hope this helps 🤙

No comments yet…

DevInTheWild.

Login To Add Comments.

Want More Like This?

Subscribe to occasional updates.