Container Name Already in Use
A Docker container with the specified name already exists.
Explanation
Container name already in use error occurs when you try to create a new Docker container with a name that is already assigned to an existing container (whether running or stopped). Docker container names must be unique across all containers on the system. Common causes include running docker run multiple times without removing the old container, using fixed container names in scripts, or trying to recreate a container without first removing or renaming the existing one.
Common Causes
- Previous container not removed
- Fixed container name in scripts
- docker-compose not used
- Container stopped but not removed
- Multiple run attempts without cleanup
Solution
Remove the existing container before creating a new one: docker rm container_name. Or use docker rm -f to force-remove a running container. Use docker ps -a to list all containers including stopped ones. Use the --rm flag with docker run to automatically remove the container when it stops. For docker-compose, use docker-compose down before docker-compose up. Use unique names with timestamps or random suffixes. Use docker-compose which handles container lifecycle automatically.
Code Example
# This fails if container 'myapp' already exists
docker run --name myapp -d myimage
# Error: Conflict. The container name "/myapp" is already in use
# Fix: remove existing container first
docker rm -f myapp
# Then create new one
docker run --name myapp -d myimage
# Or use --rm to auto-remove when stopped
docker run --rm --name myapp -d myimage
# List all containers (including stopped)
docker ps -a
# For docker-compose
docker-compose down
docker-compose up -d
# Use dynamic names with random suffix
docker run --name myapp-$(openssl rand -hex 4) -d myimage
# Or use docker-compose which handles naming
docker-compose up -d # auto-names containers
Error Information
Language
bash
Framework
docker
Difficulty
Beginner
Views
164
Related Errors
502
DevOps
Nginx 502 Bad Gateway
Nginx received an invalid response from the upstream server (PHP-FPM, Node.js, etc.).
ENOTFOUND
DevOps
Node.js ENOTFOUND
DNS lookup failed because the hostname could not be resolved.
DevOps
Docker Permission Denied
The current user doesn't have permission to run Docker commands.
DevOps
Kubernetes OOMKilled
A container was killed by Kubernetes because it exceeded its memory limit.