Docker Error: The container name is already in use
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
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.
So this naming conflict typically happens when you try to create a Docker container with a name that already exists on the system.
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
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
version: "3" services: my_compose_service: build: context: ./MyFolder dockerfile: ./Dockerfile container_name: my_new_container_name
Hopefully this gets you unblocked and running with Docker!