Docker Error: The container name is already in use

Date: 2022-11-27 | docker | error | troubleshooting


When git cloning code repositories using Docker and spinnning them up with Docker Compose, I often will get the error:

ERROR: for CONTAINER_NAME  Cannot create container for service CONTAINER_NAME: Conflict. The container name "/CONTAINER_NAME" is already in use by container "CONTAINER_ID". You have to remove (or rename) that container to be able to reuse that name.

The fix for this is pretty simple but I run into it quite a lot when using the CloudSeed SvelteKit / F# project boilerplate since I use it as the base for most of my projects and everything's containerized in Docker.

In this post, we'll briefly explain what this error means and how to resolve it.

Solving Container Name Conflicts

The Problem

Docker uses several methods of identifying a container:

  • Name - The "category" of a container image (like my_website, my_console_app, etc)
  • ID - The unique instance of a container image (i.e. my_container_v1, my_container_v2, my_container_v3 etc but using a long GUID instead e.g. e673fd9309f85310a5df3681907439f70ff943a46190d5db261af8f3ae509d34)

So this naming conflict typically happens when you try to create a Docker container with a name that already exists on the system.

The Solution

So to solve this naming conflict problem we have a few options:

Solution 1: Delete the Container and recreate

If you are intentionally trying to create a new version of the existing container then this makes sense.

You can delete from CLI or Docker Hub if you have that installed on your machine and then rerun your command to create your container.

Delete Docker container from CLI:

docker ps -a // This will list all containers
docker rm CONTAINER_NAME // This will remove the container

Delete Docker container from Docker Hub:

  • Open Docker Hub
  • Navigate to Containers / Apps
  • Find the container you wish to delete in the list
  • Click the Delete button (usually looks like a trashcan icon)

Solution 2: Use a different container name

If the container you are trying to create is different from the existing container it shares a name with, then you should change the name. This goes back to core software principles where we want everything to be named idiomatically - easy to tell what it does and uniquely identifiable.

The method of creating the container with a different name will differ based on how you're creating it. Containers are commonly created via the Docker CLI and via Docker Compose so we'll cover these here:

To create a container with a set name via Docker CLI, utilize the --name flag:

docker run --name my_new_container_name

If you're using Docker Compose (which is how CloudSeed manages its containers), we can utilize the container_name field in docker-compose.yml:


version: "3"
      context: ./MyFolder
      dockerfile: ./Dockerfile
    container_name: my_new_container_name

Next Steps

Hopefully this gets you unblocked and running with Docker!

Further Reading: