Fix: Docker container name already in use
Quick Answer
How to fix Docker 'container name already in use by container' error caused by stopped containers, name conflicts, compose restarts, and stale container state.
The Error
You run docker run or docker-compose up and get:
docker: Error response from daemon: Conflict. The container name "/my-app" is already in use
by container "abc123def456". You have to remove (or rename) that container to be able to reuse
that name.Or from Docker Compose:
Creating my-project_web_1 ... error
ERROR: for web Cannot create container for service web: Conflict.
The container name "/my-project_web_1" is already in useDocker requires unique container names. A container with the specified name already exists — either running or stopped.
Why This Happens
Every Docker container has a unique name. When you use --name my-app or Docker Compose assigns a name, Docker checks for existing containers with that name. Stopped containers still occupy the name until they are removed.
Common causes:
- Previous container was stopped but not removed.
docker stopdoes not delete the container. - Crashed container left behind. A container that exited with an error still occupies the name.
- Docker Compose recreate conflict. Compose cannot recreate a container if the old one still exists.
- Multiple terminal sessions. You ran
docker run --name my-apptwice without removing the first. - Script or CI/CD pipeline rerun. A deployment script runs
docker run --nameon each deployment without cleaning up old containers.
Fix 1: Remove the Existing Container
The most common fix. Remove the old container so you can reuse the name:
docker rm my-appIf the container is still running, stop it first:
docker stop my-app
docker rm my-appOr force remove a running container:
docker rm -f my-appThen run your new container:
docker run --name my-app my-imagePro Tip: Use
docker rm -fin scripts and CI/CD pipelines. It removes the container whether it is running, stopped, or already removed (the-fflag does not error on non-existent containers in newer Docker versions).
Fix 2: Use —rm for Auto-Cleanup
Add --rm to docker run so the container is automatically removed when it stops:
docker run --rm --name my-app my-imageThis is ideal for:
- Development and testing
- One-off commands
- CI/CD pipeline steps
The container is deleted as soon as it exits, so the name is immediately available for reuse.
Note: --rm removes the container and its anonymous volumes. Do not use it if you need to inspect the container’s logs or filesystem after it stops.
Fix 3: Fix Docker Compose Conflicts
Docker Compose manages container names automatically. If you get name conflicts:
Bring everything down cleanly:
docker-compose down
docker-compose up -ddocker-compose down stops and removes containers, networks, and default volumes. This ensures a clean state.
Remove orphaned containers:
docker-compose down --remove-orphansIf the project name changed (directory was renamed):
# Specify the old project name to clean up
docker-compose -p old-project-name down
# Then start with the new name
docker-compose up -dForce recreate containers:
docker-compose up -d --force-recreateThis rebuilds containers even if their configuration has not changed.
Common Mistake: Using
docker-compose stopinstead ofdocker-compose downbetween deployments.stoponly stops containers — it does not remove them. The nextupfinds the old containers and tries to start them rather than creating new ones. Usedownfor a clean state.
Fix 4: Use Unique Container Names
If you need to run multiple instances, use unique names:
With timestamps:
docker run --name "my-app-$(date +%s)" my-imageWith random suffixes:
docker run --name "my-app-$(head /dev/urandom | tr -dc a-z0-9 | head -c 6)" my-imageWithout a name (Docker assigns a random name):
docker run my-image
# Docker assigns a name like "hungry_einstein"If you do not need to reference the container by name later, omit --name entirely.
Fix 5: Script-Safe Container Cleanup
In deployment scripts, always clean up before creating:
#!/bin/bash
CONTAINER_NAME="my-app"
# Stop and remove if exists (ignore errors)
docker stop "$CONTAINER_NAME" 2>/dev/null || true
docker rm "$CONTAINER_NAME" 2>/dev/null || true
# Start fresh
docker run -d --name "$CONTAINER_NAME" my-imageOr use docker run --replace (Docker 25.0+):
docker run --replace --name my-app -d my-imageThe --replace flag automatically stops and removes any existing container with the same name before creating the new one. This is the cleanest solution for deployment scripts.
Fix 6: Clean Up All Stopped Containers
Remove all stopped containers at once:
docker container pruneOr with force (no confirmation prompt):
docker container prune -fRemove containers older than 24 hours:
docker container prune --filter "until=24h"List all stopped containers first:
docker ps -a --filter "status=exited"Remove all stopped containers manually:
docker rm $(docker ps -aq --filter "status=exited")For disk space issues from accumulated containers and images, see Fix: Docker no space left on device.
Fix 7: Use Docker Compose Restart Policies
Instead of manually managing container names, use Compose with restart policies:
services:
web:
image: my-app:latest
restart: unless-stopped
container_name: my-app # Optional — explicit nameRestart policies:
| Policy | Behavior |
|---|---|
no | Never restart (default) |
always | Always restart, including after Docker daemon restart |
unless-stopped | Restart unless manually stopped |
on-failure | Restart only on non-zero exit code |
With restart: unless-stopped, the container automatically restarts after crashes or Docker daemon restarts. You rarely need to docker run again.
Fix 8: Rename Instead of Remove
If you want to keep the old container for debugging:
docker rename my-app my-app-old
docker run --name my-app my-imageLater, inspect the old container:
docker logs my-app-old
docker inspect my-app-oldAnd remove it when done:
docker rm my-app-oldStill Not Working?
If the error persists after removing the container:
Check for the container in a different Docker context:
docker context ls
docker context use defaultCheck for Swarm service conflicts. In Docker Swarm mode, services have their own naming. A service container might conflict with a standalone container name.
Check for Docker Desktop bug. On macOS/Windows, Docker Desktop occasionally has stale state. Restart Docker Desktop:
# macOS
osascript -e 'quit app "Docker"' && open -a Docker
# Windows
Restart-Service dockerCheck for permission issues on the Docker socket. If you see permission errors when trying to remove containers, see Fix: Docker permission denied socket.
Check for port conflicts. If the name is free but the container still cannot start, the port might be in use. See Fix: Docker port is already allocated.
If the Docker daemon itself is not running, see Fix: Docker daemon is not running.
Solo developer based in Japan. Every solution is cross-referenced with official documentation and tested before publishing.
Was this article helpful?
Related Articles
Fix: Docker container health status unhealthy
How to fix Docker container health check failing with unhealthy status, including HEALTHCHECK syntax, timing issues, missing curl/wget, endpoint problems, and Compose healthcheck configuration.
Fix: Docker build sending large build context / slow Docker build
How to fix Docker build sending large build context caused by missing .dockerignore, node_modules in context, large files, and inefficient Dockerfile layers.
Fix: Docker Build Cache Not Working - No Cache Being Used
How to fix Docker build cache not working when layers rebuild every time despite no changes, including layer ordering, .dockerignore, COPY invalidation, BuildKit cache mounts, and CI/CD cache strategies.
Fix: Docker Compose Service failed to build / ERROR building
How to fix Docker Compose Service failed to build errors caused by wrong Dockerfile paths, YAML syntax issues, build args, platform mismatches, and network failures.