docker run fails because the container name is already taken
docker: Error response from daemon: Conflict. The container name "/my-app" is already in use by containerShort answer
Container names are unique on one Docker host. A stopped container keeps its name until you remove it, so a second docker run with the same --name is refused. List all containers with that name, then remove the old one, rename it, or add --rm so the name frees itself when the container exits.
Fix it fast
1Find out which cause you have
List every container, running or stopped, whose name contains yours. The default docker ps hides stopped ones, which is why the clash looks like a mystery.
docker ps -a --filter name=my-app2Match what you see, then jump to its fix
- 1
docker ps shows nothing, but docker ps -a lists my-app with status ExitedAn old, stopped container still owns the nameFix - 2
docker ps lists my-app with status UpThe old container is still running and you want to replace itFix - 3
The first run works and every later run fails with the conflictA script or CI job runs docker run with the same name every timeFix
- 1
What does the error look like?
docker: Error response from daemon: Conflict. The container name "/my-app" is already in use by containerYou have to remove (or rename) that container to be able to reuse that name.
Cause 1 of 3
An old, stopped container still owns the name
1Check
The name filter matches part of a name, and -a includes stopped containers, so this shows what is holding the name.
docker ps -a --filter name=my-app --format "{{.ID}} {{.Names}} {{.Status}}"2Fix
Remove the stopped container, then run the new one. If the old container holds data you still need, copy it out or rename the container instead (next cause).
docker rm my-app
docker run -d --name my-app my-image:latest3Verify
The new container is listed as running under that name.
docker ps --filter name=my-appConfirmed by docs.docker.com
Cause 2 of 3
The old container is still running and you want to replace it
1Check
A running container cannot be removed with a plain docker rm. Decide first whether it should be replaced or kept alongside the new one.
docker ps --filter name=my-app2Fix
To replace it, force-remove it (this sends SIGKILL, so stop it cleanly first if it writes data). To keep it, give it a new name and free the old one.
# replace
docker stop my-app && docker rm my-app
# or keep it under another name
docker rename my-app my-app-old3Verify
Run the new container with the original name.
docker run -d --name my-app my-image:latestConfirmed by docs.docker.com
Cause 3 of 3
A script or CI job runs docker run with the same name every time
1Check
Look for docker run --name in your deploy script. Without --rm, each finished container stays behind and keeps the name.
grep -rn "docker run" deploy.sh .github/workflows 2>/dev/null2Fix
For short-lived containers add --rm so Docker deletes the container and frees the name on exit. For long-running ones, remove the previous container in the script before starting the new one. Note that --rm cannot be combined with --restart.
docker run --rm --name my-job my-image:latest
# long-running service: replace explicitly
docker rm -f my-app 2>/dev/null || true
docker run -d --name my-app my-image:latest3Verify
Run the script twice in a row; the second run must not fail.
./deploy.sh && ./deploy.sh
docker ps -a --filter name=my-appConfirmed by docs.docker.com
How do I stop it coming back?
- Use --rm for one-off containers so no stopped container is left holding a name.
- Let Docker Compose own long-running services; omit container_name so Compose generates names and can scale the service.
- Remember the name filter matches part of a name, so check the full name in the output before running docker rm.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Servers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
sam local fails: Running AWS SAM projects locally requires Docker
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
sam local start-api and sam local invoke run your function inside a Docker container, so SAM stops if it cannot talk to Docker.
3 causes, each with a fixServers
dnf fails to download repo metadata in a CentOS 8 Docker build
The same Dockerfile built fine before 2022
CentOS Linux 8 reached end of life on 31 December 2021 and its content left the mirrors.
2 causes, each with a fixServers
pip install confluent-kafka fails because librdkafka/rdkafka.h is missing
pip stops while building confluent-kafka from source
The error means pip is compiling confluent-kafka from source, and the compiler cannot find the librdkafka C headers.
3 causes, each with a fixServers
Cypress says the npm package is installed but the binary is missing
Cypress binary is missing after npm install in CI or in a Docker build
The npm package is only a launcher.
3 causes, each with a fixServers
A dev container port does not open in the browser when Docker runs on a remote host
If your application only accepts calls from localhost, it will reject connections from published ports
Three things usually break it: the app listens only on localhost inside the container, so published ports are rejected; you open the container port instead of the mapped localhost port VS Code shows; or you changed appPort or a Compose ports list and did not rebuild.
3 causes, each with a fix