Docker: Container Name Already in Use (tested)
Docker container names must be unique on a host, even for containers that are stopped. This error fires the instant you try to docker run --name X while any container — running or not — already holds that name.
1. Reproduced: the exact error
With one container already named forever-name-test, trying to create a second container with the same name fails before it starts:
docker run -d --name forever-name-test postgres:16-alpine
docker: Error response from daemon: Conflict. The container name
"/forever-name-test" is already in use by container
"65dbe2e2f387...". You have to remove (or rename) that container
to be able to reuse that name.
Note that the message says "already in use" even if the existing container is stopped, not just running — a name is reserved as long as the container exists at all.
2. Fix 1: remove the old container, then reuse the name
If the old container is disposable, remove it and the name becomes available immediately:
docker rm forever-name-test
docker run -d --name forever-name-test postgres:16-alpine
f71a07a8a8af...
docker ps --filter name=forever-name-test --format "{{.Names}} {{.Status}}"
forever-name-test Up Less than a second
We confirmed this: the new container started cleanly with the exact same name the moment the old one was removed. If the old container is still running, docker rm alone will refuse — stop it first (docker stop) or use docker rm -f to force both in one step.
3. Fix 2: don't recreate it — start the existing one
If the existing container is exactly what you want and it's just stopped, you don't need a new container at all — start the one you already have:
docker start forever-name-test
This is the right fix when the error shows up from a script that blindly runs docker run every time instead of checking whether the container already exists. Recreating a container loses anything written to its writable layer (unless it's using volumes); starting it keeps that state.
4. Fix 3: give the new container a different name
If you genuinely need both the old and the new container to exist side by side — for example, comparing two versions — skip the conflict entirely with a different --name:
docker run -d --name forever-name-test-v2 postgres:16-alpine
5. Scripting tip: check before creating
For automation that re-runs regularly, check for the name first and branch instead of always calling docker run and catching the error:
if docker ps -a --format '{{.Names}}' | grep -qx forever-name-test; then
docker start forever-name-test
else
docker run -d --name forever-name-test postgres:16-alpine
fi
This avoids the error entirely rather than reproducing it on every script run and discarding the output.
Frequently asked questions
Does this happen even if the old container is stopped?
Yes. We confirmed the name conflict fires for a stopped container just as it does for a running one — Docker reserves the name as long as the container object exists, regardless of its run state.
What's the fastest fix if I don't care about the old container's data?
docker rm -f <name> followed by the same docker run command. We tested docker rm on a stopped container and the name was reusable immediately afterward with no delay.
How do I keep the old container's data instead of recreating it?
Use docker start <name> instead of docker run. We confirmed this brings the existing container back up without creating anything new, so its writable-layer changes and any mounted volumes are preserved.
Can two containers share the same name if they're in different states?
No. We tested this directly: Docker rejected the second docker run purely because the name existed, without even checking whether the first container was running or stopped.
How do I avoid hardcoding retry logic for this in scripts?
Check docker ps -a --format '{{.Names}}' for the name first and branch to docker start or docker run accordingly, rather than always attempting docker run and handling the conflict after the fact.
Written with AI assistance. Measured on 2026-10-03 with Docker Engine on Linux, using real containers (postgres:16-alpine). Every error message below is copy-pasted output, not paraphrased. PlainHeaders is our extension; Docker is free.