Docker: "Conflict. The Container Name Is Already in Use"

A stopped (or running) container already holds the name — names are unique regardless of state. Decide deliberately: reuse the existing one, or remove it before recreating. --force on rm is where foot-guns live.

What you'll see

Root causes

Previous container still exists (stopped)

Stopping doesn't remove: docker ps -a lists it. Names persist until the container is removed, so every re-run of a --name'd docker run collides with the earlier attempt.

Auto-restart policies hiding the container

A container with restart=always that failed may look dead in ps but still hold the name. Or another compose project pinned the name explicitly.

Fix it

  1. See what holds the name
    docker ps -a --filter name=<name> --format 'table {{.ID}}\t{{.Status}}\t{{.Image}}'
  2. If it's the old copy of the same thing: remove it
    docker rm -f <name>   # -f stops it first if running
  3. If it should keep running: interact with it instead of recreating
    docker exec -it <name> sh   # or docker start <name> if stopped
  4. Idempotent scripts: clean before create
    docker rm -f <name> 2>/dev/null; docker run -d --name <name> ...   # or use compose, which recreates by definition

Field note

docker run --rm removes on exit but does nothing for a previous container that still holds the name — the cleanup has to target the OLD container, not the new run. Compose manages this by design: up -d recreates. Manual docker run scripts need the rm-first pattern or random/volatile names.

Common questions

Why not just omit --name and let Docker pick one?

Random names make everything downstream harder (logs, exec, networking by name). Deterministic names + explicit cleanup is the stable pattern; random names trade a collision for an unfindable container.

Is docker rm -f dangerous?

Only to that container: -f stops a running container to remove it. If something's genuinely serving traffic under that name, you just killed production — filter first, force second.

Ship it right the first time

A production-shaped compose stack: healthchecks, resource limits, log rotation. Never debug a boot race again.

Docker Production Starter — $19 →

One-time. Yours to modify. Instant download from the NinjaOps template store.