What the error means
Docker ensures container names are unique per daemon. The error "Error response from daemon: conflict: container name is already in use" occurs when you try to create a container with a name that already belongs to another container (running or stopped).
Common causes:
- A previous container with that name is stopped but not removed.
- A running container already uses that name.
- Docker Compose created a container with the same name (via container_name or project name).
Quickstart (fastest fixes)
- Reuse the container: docker start web
- Or delete it if not needed: docker rm -f web
- Or rename the existing one: docker rename web web-old
- With Compose: docker compose down (in the project directory), then up again
Minimal working example (reproduce and fix)
# Create a container named "web"
docker run -d --name web nginx:alpine
# Attempt to create another container with the same name -> error
docker run -d --name web nginx:alpine
# Error response from daemon: conflict: container name is already in use
# Fix option A: start existing container if that was intended
docker start web
# Fix option B: stop and remove it, then re-create
docker stop web && docker rm web
# Now this works again
docker run -d --name web nginx:alpine
Step-by-step diagnosis
- Find the conflicting container by exact name
# Show any container whose name exactly matches "web"
docker ps -a --filter "name=^web$" --format 'table {{.ID}}\t{{.Status}}\t{{.Names}}'
- Decide what to do
- If you want to use the existing container: start it.
- If you want a fresh container: remove the existing one.
- If you need both: rename the existing one, then create the new container.
- Apply the action
# Start existing
docker start web
# Stop then remove (safe if you know it’s the right one)
docker stop web && docker rm web
# Force remove if it won’t stop
docker rm -f web
# Rename to free the name
docker rename web web-old
- Verify
docker ps -a --format '{{.Names}}\t{{.Status}}'
Docker Compose specifics
Conflicts often happen when Compose reuses names.
- Default naming: <project><service><index>. The project defaults to the directory name.
- Explicit names: container_name in compose files forces a specific name and can collide.
Common fixes:
# In the Compose project directory
# See containers
docker compose ps -a
# Stop and remove project containers
docker compose down
# If volumes must be cleared (data loss!)
# docker compose down -v
# Bring it back up
docker compose up -d
# Use a different project name (avoids clashes across repos)
docker compose -p myproj up -d
If you use container_name in multiple projects, ensure uniqueness or remove the directive to let Compose auto-name.
Scenarios and recommended commands
- I just want to reuse the container:
- docker start NAME
- I need a fresh one with that name:
- docker rm -f NAME; then docker run --name NAME ...
- I need to keep the old container for later:
- docker rename NAME NAME-old; then create the new container
- CI pipeline or temp jobs:
- Use --rm so containers auto-delete on exit
- Compose project name collision:
- docker compose -p unique up -d or docker compose down before up
Preventing future conflicts
- Avoid hard-coding names unless necessary; let Docker auto-name.
- For ephemeral runs, use --rm to auto-remove on exit:
docker run --rm --name my-task alpine:3.20 sh -c 'echo done'
- In CI, suffix names with unique IDs (e.g., BUILD_ID) or use Compose with unique -p.
- Periodically prune stopped containers:
docker container prune -f
- With Compose, avoid container_name unless you truly need a stable name.
Pitfalls to avoid
- Removing the wrong container: always confirm with docker ps -a --filter "name=^NAME$".
- Using -f blindly: docker rm -f kills running workloads; ensure it’s safe.
- Data loss with -v or compose down -v: volumes are permanent storage; removing them deletes data.
- Different Docker context/daemon: a name may be in use on a different context. Check with docker context ls and docker context use.
- Name vs network alias: network aliases don’t conflict with container names.
Performance notes
- docker rm is fast; docker rm -f adds stop overhead if the container is busy.
- Removing large containers with many attached volumes can take longer; avoid -v unless needed.
- docker container prune is efficient for many stopped containers but can block briefly while cleaning metadata.
- Compose down/up cycles are costlier for large stacks; prefer rolling updates (recreate individual services) when possible.
Troubleshooting checklist
- List containers by exact name:
docker ps -a --filter "name=^NAME$" --format '{{.ID}}\t{{.Names}}\t{{.Status}}'
- If present, decide: start, remove, or rename.
- With Compose, run docker compose ps -a; then docker compose down before up.
- If nothing shows but error persists, confirm Docker context and permissions.
Tiny FAQ
Why do I still get conflicts after docker rm?
- The target container may be running or replicated by Compose; run docker rm -f NAME or docker compose down.
Can I have two containers with the same name on different networks?
- No. Names are unique per Docker daemon, regardless of network.
Does a stopped container still reserve its name?
- Yes. You must remove or rename it to reuse the name.
How do I see only the ID of the conflicting container?
- docker ps -a --filter "name=^NAME$" --format '{{.ID}}'
Should I use container_name in Compose?
- Only if you need a stable, human-friendly name. Otherwise let Compose auto-name to avoid collisions.