The Docker daemon refused to start a new container because it found an existing container with the same name.

This error typically occurs when a previous container creation or run command was interrupted, leaving behind a "ghost" container entry that the Docker daemon still recognizes. The daemon is designed to prevent naming collisions to ensure each container has a unique identifier.

Here are the most common reasons this happens and how to fix them:

1. The Container Exists and is Stopped

This is the most frequent cause. A previous docker run command might have completed successfully but the container was stopped, or a docker create command was run without a subsequent docker start.

  • Diagnosis:

    docker ps -a | grep <container_name_or_id>
    

    Replace <container_name_or_id> with the name you’re trying to use or the ID if you know it. The -a flag shows all containers, including stopped ones. If you see your container listed with a status like Exited or Created, this is your culprit.

  • Fix:

    docker rm <container_name_or_id>
    

    This command removes the existing, stopped container. After removing it, you can try your docker run command again.

  • Why it works: The docker rm command explicitly tells the Docker daemon to delete the container’s definition and associated filesystem layers, clearing the name for a new container.

2. A Container with the Same Name is Currently Running

Less common if you’re encountering an "already exists" error on creation, but possible if you’re trying to run a container with a name that’s already in use by an active container.

  • Diagnosis:

    docker ps | grep <container_name_or_id>
    

    This command lists only running containers. If your container name appears here, it’s currently active.

  • Fix: You have two options: a) Stop and remove the running container: bash docker stop <container_name_or_id> docker rm <container_name_or_id> b) Choose a different name for your new container.

  • Why it works: Stopping a container halts its processes, and removing it cleans up its state. Alternatively, using a new name bypasses the collision entirely.

3. Interrupted Docker Daemon or Host Reboot

Sometimes, a Docker daemon crash, an unclean shutdown, or a host system reboot can leave behind stale container entries without the container actually being in a usable state.

  • Diagnosis:

    docker ps -a
    

    Look for any containers that appear to be in an inconsistent state, or have unusual names/IDs that you don’t recognize. Sometimes these might show up with a dead status.

  • Fix:

    docker container prune
    

    This command removes all stopped containers. If you suspect specific stale entries, you can remove them individually with docker rm.

  • Why it works: docker container prune is a garbage collection tool for Docker that cleans up orphaned container objects, effectively resolving inconsistencies left by unexpected daemon terminations.

4. Docker Swarm or Kubernetes Orchestration Conflicts

If you’re using Docker in an orchestrated environment like Docker Swarm or Kubernetes, a service definition might be trying to create a container with a name that’s already managed by the orchestrator. This is less about a direct Docker daemon conflict and more about the orchestrator’s state.

  • Diagnosis: This is highly environment-specific.

    • Docker Swarm: docker service ls, docker service ps <service_name>, and docker ps -a on the nodes involved.
    • Kubernetes: kubectl get pods, kubectl get deployments, kubectl get statefulsets, and kubectl describe pod <pod_name> for more details.
  • Fix:

    • Docker Swarm: Scale down the service (docker service scale <service_name>=0), then scale it back up, or remove the service (docker service rm <service_name>) and recreate it.
    • Kubernetes: Delete the conflicting Pod, Deployment, or StatefulSet (kubectl delete pod <pod_name>, kubectl delete deployment <deployment_name>, etc.) and let the controller recreate it.
  • Why it works: Orchestrators manage the lifecycle of containers. Recreating or scaling down/up the service definition allows the orchestrator to re-assert control and ensure unique naming within its own management scope.

5. Corrupted Docker Metadata

In rare, severe cases, the Docker daemon’s internal metadata (stored in /var/lib/docker on Linux) can become corrupted, leading to phantom container entries.

  • Diagnosis: Check Docker daemon logs for errors related to metadata access or storage:

    journalctl -u docker.service
    

    or check /var/log/docker.log. If you see errors about reading or writing to the Docker root directory, corruption is a possibility.

  • Fix: This is a more drastic step. a) Stop the Docker daemon: bash sudo systemctl stop docker b) Backup your Docker data! (especially images, volumes, and any critical container configurations). c) Remove the Docker data directory: bash sudo rm -rf /var/lib/docker d) Restart the Docker daemon: bash sudo systemctl start docker e) Recreate your containers, networks, and volumes. You will need to re-pull images.

  • Why it works: This completely resets the Docker daemon’s state, clearing out any corrupted metadata and forcing it to start with a clean slate. This is a last resort as it requires rebuilding your Docker environment.

6. Docker API Race Condition

If multiple processes or scripts are trying to create containers with the same name simultaneously, a race condition can occur. One process might get a "container exists" error while another successfully creates it, or both might fail if the check and creation aren’t atomic.

  • Diagnosis: This is hard to diagnose directly from docker ps. Look for patterns in your logs or application behavior where container creation is being initiated rapidly or concurrently.

  • Fix: Implement proper locking mechanisms in your scripts or applications that manage container creation. Ensure only one process attempts to create a container with a specific name at any given time. Alternatively, use unique names for each container instance if they are intended to be distinct.

  • Why it works: Preventing concurrent operations with shared resources (like container names) eliminates the possibility of race conditions and ensures predictable behavior.

After resolving the "Container Already Exists" error, the next common issue you might encounter is a "port is already allocated" error if the container you’re trying to start attempts to bind to a host port that’s already in use by another process or container.

Want structured learning?

Take the full Docker course →