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.
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
docker ps -a --filter name=^web$docker start web # keep it, and start it againdocker rm -f web # or remove it, then run againFor 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
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES4f2d8c1e9a7b nginx:1.27 "/docker-entrypoint.…" 2 days ago Exited (0) 5 minutes ago webdocker 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:
docker start webdocker logs -f webIt 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
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:
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:
Or, for short-lived containers, use --rm:
docker run --rm --name migrate myapp:1.4 ./migrate.sh5. 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: webThe fixes:
- Remove the old container:
docker rm -f web, thendocker compose up -d. - If two projects share a
container_name, rename one, or dropcontainer_nameand 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 downto 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:
docker container pruneIt 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.
Found this useful? Share it.
Found a mistake? Let us know at contact@ohsudo.com.