docker: Error response from daemon: Conflict. The container name "/my-container" is already in use
What causes this
Docker container names must be unique. Even after a container stops, it still exists and holds its name. When you try to docker run --name my-container and a container with that name already exists (running or stopped), Docker refuses.
This commonly happens when:
- You ran the same
docker runcommand twice - A previous run crashed but the container wasn’t removed
- You’re using Docker Compose and manually created a container with the same name
- A script creates named containers without cleaning up
Fix 1: Remove the old container
# See all containers (including stopped ones)
docker ps -a | grep my-container
# Remove it
docker rm my-container
# If it's still running, force remove
docker rm -f my-container
Fix 2: Use —rm to auto-remove on exit
# Container is automatically removed when it stops
docker run --rm --name my-container myimage
This is ideal for development and one-off tasks. The container is cleaned up as soon as it exits.
Fix 3: Stop and replace in one command
# Remove if exists, then create new
docker rm -f my-container 2>/dev/null; docker run --name my-container myimage
Useful in scripts and CI pipelines where you want idempotent deployments.
Fix 4: Use docker-compose instead
Docker Compose handles container naming and lifecycle automatically:
# docker-compose.yml
services:
app:
image: myimage
container_name: my-container
# Stops old container, removes it, starts new one
docker compose up -d
Fix 5: Clean up all stopped containers
If you have many leftover containers:
# Remove all stopped containers
docker container prune
# Or remove everything unused (containers, images, networks)
docker system prune
Related resources
How to prevent it
- Always use
--rmfor development containers so they clean up automatically - Use Docker Compose for multi-container setups — it manages container lifecycle for you
- In CI/CD scripts, add
docker rm -f container-name 2>/dev/nullbeforedocker run - Periodically run
docker container pruneto clean up stopped containers