Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

docker run fails because the container name is already taken

Errordocker: Error response from daemon: Conflict. The container name "/my-app" is already in use by container

Short 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.

  • Applies to Docker Engine 28, Docker Compose 2.40, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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-app
  2. 2Match what you see, then jump to its fix

    1. 1docker ps shows nothing, but docker ps -a lists my-app with status ExitedAn old, stopped container still owns the nameFix
    2. 2docker ps lists my-app with status UpThe old container is still running and you want to replace itFix
    3. 3The first run works and every later run fails with the conflictA script or CI job runs docker run with the same name every timeFix

What does the error look like?

  • docker: Error response from daemon: Conflict. The container name "/my-app" is already in use by container
  • You 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:latest

3Verify

The new container is listed as running under that name.

docker ps --filter name=my-app

Confirmed by docs.docker.com

Did this fix it?

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-app

2Fix

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-old

3Verify

Run the new container with the original name.

docker run -d --name my-app my-image:latest

Confirmed by docs.docker.com

Did this fix it?

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/null

2Fix

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:latest

3Verify

Run the script twice in a row; the second run must not fail.

./deploy.sh && ./deploy.sh
docker ps -a --filter name=my-app

Confirmed by docs.docker.com

Did this fix it?

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

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.