Docker: Conflict. The container name is already in use
Names are unique per daemon and stopped containers keep theirs. Inspect labels, remove or rename the holder, and stop hardcoding container_name.

Introduction & Quick Summary
docker: Error response from daemon: Conflict. The container name "/api" is already
in use by container "3f9c2b7a1d54...". You have to remove (or rename) that container
to be able to reuse that name.Docker container names are unique per daemon, not per image, per directory or per project. The name /api is still held by a container that exists in some state — running, exited, created, or dead — and a new container cannot take it until that one is renamed or removed.
Four situations account for nearly all occurrences:
- A stopped container survived.
docker pshides it;docker ps -ashows it withExitedstatus. - Compose project mismatch. The directory was renamed, or
-p/COMPOSE_PROJECT_NAMEchanged, so Compose no longer recognizes the old containers as its own. - Hardcoded
container_name. Two Compose projects in the same repository both declarecontainer_name: postgres, and Compose's project scoping cannot save you. - CI parallelism. Two jobs on one runner both create
--name test-db.
| State of the holder | Visible in docker ps | Correct action |
|---|---|---|
| Running | Yes | Stop, then remove or rename |
| Exited | No | Remove or rename |
| Created | No | Remove |
| Dead | No | docker rm -f |
| Compose-managed | Yes, with project label | docker compose down in that project |
| Swarm task | Sometimes | docker service rm or scale down |
Core Mechanics, Signatures & Reproducible Setup
The daemon keeps a name index in /var/lib/docker/containers; docker run --name x and Compose's generated names both write to it. Generated Compose names are <project>-<service>-<index> in Compose v2 and <project>_<service>_<index> in v1, where <project> defaults to the directory basename. Renaming a directory therefore creates a new namespace of names while the old containers still hold theirs.
Reproduce the conflict without any application code:
docker run -d --name api nginx:alpine
docker stop api
docker run -d --name api nginx:alpine
# docker: Error response from daemon: Conflict. The container name "/api" is already in useDiagnose which container holds the name, including stopped ones:
docker ps -a --filter "name=^/api$" \
--format 'table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Image}}\t{{.Labels}}'The ^/api$ filter is important: --filter name=api matches api, api-db, my-api and anything else containing the substring, which leads to deleting the wrong container.
Resolve without losing data:
# rename keeps the container and its writable layer
docker rename api api-old
# removal is permanent for the container layer
docker rm apiA container's anonymous volumes survive docker rm unless you pass -v; named volumes always survive. Image data is never touched by container removal, so the only loss from docker rm is whatever was written inside the container's writable layer.
For Compose, inspect the labels before deciding:
docker inspect api --format '{{ index .Config.Labels "com.docker.compose.project" }}'
docker inspect api --format '{{ index .Config.Labels "com.docker.compose.service" }}'If the project label is a directory name you no longer use, the container is an orphan from the old project.
Key Comparison Table
| Command | Scope | Removes volumes | Use when |
|---|---|---|---|
docker rm <name> | One stopped container | No | A single stale container |
docker rm -f <name> | One container, any state | No | Dead or stuck container |
docker rm -v <name> | One container | Anonymous only | Test artifacts |
docker compose down | Current project | No | Normal teardown |
docker compose down -v | Current project | Named and anonymous | Reset local database |
docker compose down --remove-orphans | Current project | No | Renamed directory or service |
docker container prune | All stopped containers | No | Cleanup after experiments |
docker rename <old> <new> | One container | Keeps everything | Preserve state under a new name |
| Cause | Detection | Durable fix |
|---|---|---|
| Stopped container | docker ps -a shows it | docker rm or --rm at run time |
| Directory rename | Project label differs from basename | COMPOSE_PROJECT_NAME pinned |
Hardcoded container_name | Two compose files declare same name | Remove container_name |
| CI parallel jobs | Fails only with concurrency | Unique name per job |
| Swarm service | docker service ls | Remove service or rename |
| Old Compose v1 names | Contains _ and _1 suffix | docker compose down once, then v2 |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
Case 1: A stopped container from yesterday
docker ps -a --filter "name=^/api$"
# 3f9c2b7a1d54 api Exited (137) 2 days ago api:latest
docker rm api
docker run -d --name api api:latestExit code 137 means the container was killed, often by the OOM killer or a host reboot. Removing and recreating is correct for stateless services; if the container carries state in its writable layer, copy it out first:
docker cp api:/app/data ./data-backupCase 2: The directory was renamed
mv ~/projects/shop-api ~/projects/store-api
cd ~/projects/store-api
docker compose up -d
# Error ... container name "/store-api-db-1" is already in use
# (the old ones are still named shop-api-*)The old containers belong to project shop-api and are not adopted by store-api. Either tear down the old project explicitly or remove orphans from the current one:
docker compose -p shop-api down
docker compose up -dTo prevent this class of failure, pin the project name in the file:
name: store-api
services:
db:
image: postgres:17Case 3: Hardcoded container_name in two projects
# project-a/compose.yaml
services:
db:
image: postgres:17
container_name: postgres # global name# project-b/compose.yaml
services:
db:
image: postgres:17
container_name: postgres # collides with project-acontainer_name opts out of project scoping entirely. Remove it and let Compose generate <project>-db-1, or make the name unique with an environment variable:
services:
db:
image: postgres:17
container_name: "${COMPOSE_PROJECT_NAME:-local}-postgres"Hardcoded names are legitimate when an external system must resolve a fixed DNS name on a user-defined network; outside that case they only create conflicts.
Case 4: CI jobs sharing a runner
# .github/workflows/test.yml
- run: docker run -d --name test-db -p 5432:5432 postgres:17Two concurrent workflows on a self-hosted runner collide on the name and on the port. Add the run id or a random suffix and let Docker assign the host port:
docker run -d --name "test-db-${GITHUB_RUN_ID}" -p 0:5432 postgres:17
docker port "test-db-${GITHUB_RUN_ID}" 5432Register a cleanup step with if: always() so a failed job does not poison the next run.
Case 5: Rename instead of delete
When the holder has data or a long startup state you want to keep, rename it and start the new container beside it:
docker rename api api-legacy
docker run -d --name api api:v2
docker exec api-legacy pg_dump -U app appdb > dump.sqldocker rename works while the container runs and does not affect published ports or networks. Only the name changes, so DNS aliases inside user-defined networks keep pointing at the old name until it is recreated.
Case 6: Swarm services and plain containers
docker service create --name api stores the name in Swarm's service namespace, while docker run --name api uses the container namespace. They do not always conflict, but tasks created by a service can appear as containers with the service name prefix, and docker run --name api may then fail on the node running the task. Check both:
docker service ls
docker ps -a --filter "name=api"Remove the service with docker service rm api, then confirm the task containers are gone before reusing the name.
Common Compilation/Runtime Mistakes & Version Gotchas
docker psalone. It shows only running containers, so a stopped holder is invisible and the error looks impossible. Always start withdocker ps -a.- Substring name filters.
--filter name=apimatchesapi-dbandmy-api; use anchored regexes (^/api$) before removing anything. down -vas a reflex. It deletes named volumes, including the local database you were debugging. Remove the conflicting container, not the whole project, unless a reset is the intent.- Compose v1 to v2 name change. Old v1 containers use
<project>_<service>_1; v2 looks for<project>-<service>-1and reports a conflict against the new name only after creating it. Tear down once with the v1-era tool ordocker rmthe old containers explicitly. --force-recreatedoes not free names. It recreates in place and can fail with the same conflict when acontainer_nameis held by a container outside the project.- Dead containers. A container in
Deadstate cannot be renamed or started; onlydocker rm -fclears it. The state usually follows a failed overlay mount or a daemon crash.
When comparing Compose files before removing a container_name, paste both into the diff tool. Validate the merged file with the docker-compose validator, which catches duplicate service keys and invalid interpolations that produce unexpected project names.
Primary references: docker container run documents the --name uniqueness rule, and Compose project names explains how the project prefix is derived and overridden.
Official references
primary documentationThe Docker documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Docker work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Docker guides
All Docker guidesDocker build: failed to compute cache key: not found
BuildKit cannot hash a file that is not in the context. Correct the context path, the .dockerignore rules, or the COPY source and stage name.
Docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock
An endpoint mismatch, a stopped daemon, or a missing socket mount. Resolve which of the three, then start, retarget, or mount.
Docker Compose: bind: address already in use and Port Conflicts
A published port is a host socket, and two projects cannot own it. How to find the real owner, why an override file merged instead of replacing, and how.
Docker Buildx: Building Multi-Architecture Images (AMD64 & ARM64) Explained
One tag, several architectures: the driver that supports it, QEMU versus cross-compilation, registry cache, and reading the manifest list.
More in Docker
All Docker guidesThe guides filed next to this one in Docker, in library order.