Skip to content
ohsudo, Home
Docker & Containers

Docker "Conflict. The container name is already in use": start it, remove it, or rename it

Fix Docker "Conflict. The container name is already in use": find the old container, then start it, remove it, or use --rm, plus the Compose case.

Beginner3 min readApplies to Docker Engine 20.10+ · Docker Desktop · Docker Compose v2

by OhSudo ·

Checked on real software: Docker, Docker Compose · November 2026How we check

Not sure this is your cause?Find it step by step: Container won't start →

Part of a learning path:Docker: from first container to production

docker: Error response from daemon: Conflict. The container name "/web" is already in use by container "4f2d8c1e9a7b...". You have to remove (or rename) that container to be able to reuse that name.

Each container name can belong to only one container, and stopped containers still exist. You ran docker run --name web once, it stopped, and now a second docker run --name web wants the same name.

So the question is just what you want to do with the old one: use it again, or replace it.

TL;DR

Terminal window
docker ps -a --filter name=^web$
docker start web # keep it, and start it again
docker rm -f web # or remove it, then run again

For containers you don’t need to keep, add --rm to docker run, and Docker removes them when they stop.

1. Look at the old container

Terminal window
docker ps -a --filter name=^web$
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
4f2d8c1e9a7b nginx:1.27 "/docker-entrypoint.…" 2 days ago Exited (0) 5 minutes ago web

docker ps alone only shows running containers, which is why it’s easy to miss. The -a shows stopped ones too.

2. Use it again

If it’s the same image and settings, start the existing container:

Terminal window
docker start web
docker logs -f web

It keeps its data, and the settings it was created with: ports, volumes, environment. To change any of those, you need a new container, as below.

3. Replace it

Terminal window
docker rm web
docker run -d --name web -p 8080:80 nginx:1.27

docker rm only removes stopped containers. docker rm -f stops and removes a running one. Its writable layer goes with it, and anything written inside the container, outside volumes, is lost. Data in named volumes stays.

Or keep the old one under another name, in case you need it:

Terminal window
docker rename web web-old

4. Scripts and one-off runs

A script that runs docker run --name each time fails on its second run. Either remove the container first, ignoring the error when it doesn’t exist:

Terminal window
docker rm -f web 2>/dev/null || true
docker run -d --name web nginx:1.27

Or, for short-lived containers, use --rm:

Terminal window
docker run --rm --name migrate myapp:1.4 ./migrate.sh

5. Docker Compose

Compose names containers itself, like myproject-web-1, and handles them on each docker compose up. You’ll only see this error when the file sets container_name, and a container with that name already exists, created by hand or by another Compose project:

services:
web:
image: nginx:1.27
container_name: web

The fixes:

  • Remove the old container: docker rm -f web, then docker compose up -d.
  • If two projects share a container_name, rename one, or drop container_name and let Compose choose, which also allows scaling.
  • If you renamed your project folder, Compose sees a new project, while the old containers still exist. Run docker compose -p oldname down to remove them.

You can practice the Compose case in the terminal: docker compose up -d stops at the old container, and starts once it’s gone.

The Docker Compose Generator writes files without fixed container names.

6. Clean up old containers

Stopped containers pile up. To remove all of them at once:

Terminal window
docker container prune

It asks before deleting, and only touches stopped containers. Check docker ps -a first if some of them matter.

Warning

docker rm -f and docker container prune delete each container’s own files. A database running without a volume keeps its data inside the container, and loses it here. Check docker inspect web --format '{{ json .Mounts }}' before removing anything that held data.

Shell, we have a problem!

Similar, but different: port is already allocated is the same idea for a port, not a name. Cannot connect to the Docker daemon means Docker isn’t running. A container that stops right after starting is exited with code 137 if it was killed. For the commands around this, see the Docker CLI Cheat Sheet. For everyday commands, see the Docker Compose cheat sheet. The docker run ⇄ Compose Converter turns your docker run commands into a compose file, and back. The same leftover containers also block removing their images.

$ ./practice.sh

Practice this in the terminal

Open the Terminal Playground with this article’s scenario loaded. It is safe and runs in your browser.

Open the terminal →

Found this useful? Share it.

Found a mistake? Let us know at contact@ohsudo.com.

Tools

Try it yourself

Articles

Related articles