This error occurs when you try to create or start a Docker container with a name that's already taken by an existing container. Even stopped containers keep their names until removed.
This error message indicates that Docker cannot create or start a new container because another container with the same name already exists on your system. Docker requires each container to have a unique name, even if the container is currently stopped. When you use the `--name` flag with `docker run`, Docker assigns that specific name to the container. If a container with that name already exists (whether running or stopped), Docker will refuse to create a new container with the same name to avoid conflicts. This is a protective measure - stopped containers aren't automatically deleted because they can be restarted and retain all their configuration, volumes, and state. Docker treats container names as unique identifiers across all containers on the host.
First, check which containers exist on your system, including stopped ones:
docker ps -aThis shows all containers with their names, IDs, and current status. Look for the container name mentioned in your error message.
Example output:
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
abc123def456 nginx ... 2 hours ago Exited (0) 2 hours ago mycontainerOnce you've identified the conflicting container, remove it by name or ID:
docker rm mycontainerOr using the container ID:
docker rm abc123def456If the container is still running, you need to stop it first:
docker stop mycontainer
docker rm mycontainerOr force remove it in one command (stops and removes):
docker rm -f mycontainerAfter removal, you can create a new container with that name.
If you want to reuse the existing container rather than create a new one:
docker start mycontainerThis is useful when:
- The container has the configuration you need
- You want to preserve volumes and state
- You're just restarting after a system reboot
To check if the container started successfully:
docker psYou can avoid the conflict by choosing a different name:
docker run --name mycontainer-2 nginxOr omit the --name flag entirely - Docker will assign a random name:
docker run nginxDocker generates names like "quirky_einstein" or "elegant_turing" automatically.
If you're running temporary or one-off containers, use the --rm flag to automatically remove the container when it stops:
docker run --rm --name mycontainer nginxThis prevents name conflicts on subsequent runs and keeps your system clean. Perfect for:
- Test runs
- One-time scripts
- Development containers that don't need persistence
Cleaning up multiple stopped containers:
To remove all stopped containers at once:
docker container pruneDocker will prompt for confirmation. Use -f to skip the prompt:
docker container prune -fYou can also filter by age:
docker container prune --filter "until=24h"Docker Compose considerations:
When using Docker Compose, container names are auto-generated from project, service, and replica number. If you've specified custom container names in your docker-compose.yml:
services:
web:
container_name: mycontainerYou'll encounter this error if the container exists. Use:
docker-compose downThis stops and removes containers defined in the compose file.
Race conditions in CI/CD:
In automated environments, use idempotent patterns:
docker stop mycontainer 2>/dev/null || true
docker rm mycontainer 2>/dev/null || true
docker run --name mycontainer nginxOr combine start/run logic:
docker start mycontainer || docker run --name mycontainer nginxContainer name format:
Valid container names must match the pattern [a-zA-Z0-9][a-zA-Z0-9_.-]*. Names are case-sensitive and cannot contain special characters except underscore, period, and hyphen.
dockerfile parse error line 5: unknown instruction: RRUN
How to fix 'unknown instruction' Dockerfile parse error in Docker
Error response from daemon: manifest for nginx:nonexistent not found: manifest unknown: manifest unknown
How to fix 'manifest for image:tag not found' in Docker
Error response from daemon: invalid reference format: repository name must be lowercase
How to fix 'repository name must be lowercase' in Docker
Error response from daemon: No such image
How to fix 'No such image' in Docker
Error response from daemon: Container is not running
How to fix 'Container is not running' when using docker exec