What the message looks like
docker: Error response from daemon: Conflict. The container name "/web" is already in use by container "3f1c0a9b7d2e". You have to remove (or rename) that container to be able to reuse that name.Common causes
- An earlier container with the same
--namewas only stopped, not removed; it still shows indocker ps -a. - A
docker runin a script or cron job runs repeatedly with a fixed name and without--rm. - In Docker Compose
container_nameis hard-coded and an old container from another project or run blocks the name. - A container is in a crash loop (restart policy) and keeps the name occupied.
- Two projects use the same name on the same host.
Check
- List all containers with that name, including stopped ones
docker ps -a --filter name=web - Details of the blocking container
docker inspect -f '{{.Name}} {{.State.Status}} {{.Config.Image}}' web - Which Compose project does it belong to?
docker inspect -f '{{ index .Config.Labels "com.docker.compose.project" }}' web - List old, exited containers
docker ps -a --filter status=exited
Fix
- If the old container is no longer needed, remove it:
docker rm web, ordocker rm -f webif it is running. - If it should stay, rename it:
docker rename web web-old, then start the new one. - To reuse the existing container, just run
docker start webinstead ofdocker runagain. - For one-off runs use
docker run --rm --name web …so the container is removed when it ends. - With Compose run
docker compose down(optionally with--remove-orphans) or dropcontainer_nameso Compose assigns names itself.
Typical scenario
A deploy script runs docker run -d --name web my-image on every run. The second deploy aborts with the name conflict because the container from the first run still exists. The script should run docker rm -f web 2>/dev/null || true first, or better move to Compose with docker compose up -d, which replaces the container itself.
FAQ
Will I lose data when I remove it?
Data in the container filesystem, yes. Named volumes and bind mounts survive docker rm; anonymous volumes only survive as long as you do not pass -v.
Why is the container missing from docker ps?
That command only lists running containers. Stopped ones show up with docker ps -a, and they still block the name.
Does docker compose up -d replace the container automatically?
Yes, when the image or configuration changed, Compose recreates the container. If nothing changed it stays as is, and a name conflict only arises with foreign containers carrying the same name.
Can I run two containers with the same name?
No, the name is unique per host. With Compose, services get unique names from project, service and index, as long as container_name is not hard-coded.
How do I clean up all exited containers?
Use docker container prune. It removes all stopped containers after asking for confirmation; running ones are not touched.