The Exact Error
docker: Error response from daemon: Conflict. The container name "/myapp" is already in use
by container "a3f8b2c1d4e5f6...". You have to remove (or rename) that container to be able
to reuse that name.
Quick summary: A stopped container still owns the name you're trying to use. Docker container names are globally unique ? even stopped containers hold their name until explicitly deleted with
docker rm.
Why This Error Happens
When you run docker run --name myapp myimage, Docker creates a container named myapp. When the container stops (exits), it enters an "Exited" state ? but it's still there. The container, its filesystem, and its name all persist until you explicitly delete it with docker rm.
Most common scenario: you ran docker run --name myapp yesterday, the container stopped, and today you try to run it again. Docker sees that myapp already exists ? stopped or not ? and refuses to create a new container with the same name.
Step-by-Step Diagnosis
Step 1 ? List all containers including stopped ones
docker ps -a
Look for your container name in the NAMES column with Exited status.
Step 2 ? Filter by the specific name
docker ps -a --filter "name=myapp"
Step 3 ? Check logs before removing
docker logs myapp
Exited (0) = clean exit, higher = error. Check logs before removing if you need to debug.
Solutions
Solution 1 ? Remove the old container then re-run
docker stop myapp # Stop if somehow still running
docker rm myapp # Delete the container
docker run --name myapp ...
Solution 2 ? Force remove in one command
docker rm -f myapp
Solution 3 ? Use --rm for stateless containers
docker run --rm --name myapp myimage
--rm automatically deletes the container when it exits. Perfect for dev containers and one-off scripts.
Solution 4 ? Script for safe re-deployment
#!/bin/bash
docker rm -f myapp 2>/dev/null || true
docker run -d --name myapp --restart unless-stopped myimage:latest
Solution 5 ? Migrate to Docker Compose (permanent fix)
services:
myapp:
image: myimage:latest
restart: unless-stopped
docker compose up -d # Creates/recreates automatically
docker compose down # Removes containers cleanly
Real-World Examples
Example 1: Development workflow conflict
docker run --name api-dev -p 3000:3000 myapi:dev
# Ctrl+C ? container exits but isn't removed
docker run --name api-dev -p 3000:3000 myapi:dev
# Error: container name "/api-dev" is already in use
# Fix: add --rm
docker run --rm --name api-dev -p 3000:3000 myapi:dev
Example 2: CI/CD pipeline failure
The previous CI job crashed before cleanup. Fix:
docker rm -f test-runner 2>/dev/null || true
docker run --name test-runner myimage npm test
Quick Reference
| Symptom | Likely Cause | Fix |
|---|---|---|
| Error on docker run --name | Container with that name exists | docker rm -f containername |
| Container exists but not running | Previous run exited | docker rm containername |
| CI/CD pipeline fails on name conflict | No cleanup between runs | Add `docker rm -f name 2>/dev/null |
| Recurring conflicts in production | Manual docker run workflow | Migrate to Docker Compose |
Prevent This Error in the Future
1. Use --rm for any container you don't need to persist ? dev containers, test runners, one-off scripts.
2. Adopt Docker Compose for production workloads. up/down lifecycle eliminates name conflicts entirely.
3. Add defensive cleanup at the start of deploy scripts ? docker rm -f myapp 2>/dev/null || true.
Use ToolNinja to Debug Faster
The Docker Run to Compose Converter instantly translates any docker run command into a production-ready docker-compose.yml. Compose's lifecycle management means you'll never see this name conflict error again.
?? Docker Run to Compose ? toolninja.io/tools/docker-run-to-compose