Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readDocker
#Docker#Compose#Troubleshooting#CLI
Two Docker container crates fighting for one cyan name slot, an amber compose project plate beside them

Introduction & Quick Summary

text
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 ps hides it; docker ps -a shows it with Exited status.
  • Compose project mismatch. The directory was renamed, or -p/COMPOSE_PROJECT_NAME changed, so Compose no longer recognizes the old containers as its own.
  • Hardcoded container_name. Two Compose projects in the same repository both declare container_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 holderVisible in docker psCorrect action
RunningYesStop, then remove or rename
ExitedNoRemove or rename
CreatedNoRemove
DeadNodocker rm -f
Compose-managedYes, with project labeldocker compose down in that project
Swarm taskSometimesdocker 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 use

Diagnose which container holds the name, including stopped ones:

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

bash
# rename keeps the container and its writable layer docker rename api api-old # removal is permanent for the container layer docker rm api

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

bash
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

CommandScopeRemoves volumesUse when
docker rm <name>One stopped containerNoA single stale container
docker rm -f <name>One container, any stateNoDead or stuck container
docker rm -v <name>One containerAnonymous onlyTest artifacts
docker compose downCurrent projectNoNormal teardown
docker compose down -vCurrent projectNamed and anonymousReset local database
docker compose down --remove-orphansCurrent projectNoRenamed directory or service
docker container pruneAll stopped containersNoCleanup after experiments
docker rename <old> <new>One containerKeeps everythingPreserve state under a new name
CauseDetectionDurable fix
Stopped containerdocker ps -a shows itdocker rm or --rm at run time
Directory renameProject label differs from basenameCOMPOSE_PROJECT_NAME pinned
Hardcoded container_nameTwo compose files declare same nameRemove container_name
CI parallel jobsFails only with concurrencyUnique name per job
Swarm servicedocker service lsRemove service or rename
Old Compose v1 namesContains _ and _1 suffixdocker 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:latest

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

bash
docker cp api:/app/data ./data-backup

Case 2: The directory was renamed

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

bash
docker compose -p shop-api down docker compose up -d

To prevent this class of failure, pin the project name in the file:

name: store-api services: db: image: postgres:17

Case 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-a

container_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

yaml
# .github/workflows/test.yml - run: docker run -d --name test-db -p 5432:5432 postgres:17

Two 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}" 5432

Register 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.sql

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

bash
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 ps alone. It shows only running containers, so a stopped holder is invisible and the error looks impossible. Always start with docker ps -a.
  • Substring name filters. --filter name=api matches api-db and my-api; use anchored regexes (^/api$) before removing anything.
  • down -v as 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>-1 and reports a conflict against the new name only after creating it. Tear down once with the v1-era tool or docker rm the old containers explicitly.
  • --force-recreate does not free names. It recreates in place and can fail with the same conflict when a container_name is held by a container outside the project.
  • Dead containers. A container in Dead state cannot be renamed or started; only docker rm -f clears 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 documentation

The Docker documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Docker work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Docker guides

More in Docker

All Docker guides

The guides filed next to this one in Docker, in library order.