The Exact Error
docker: Error response from daemon: Conflict. The container name "/myapp" is already in use
by container "a3f8b2c1d4e5f6...". You have to remove (or rename) that container to be able
to reuse that name.
Quick summary: A stopped container still owns the name you're trying to use. Docker container names are globally unique ā even stopped containers hold their name until explicitly deleted with
docker rm.
Why This Error Happens
When you run docker run --name myapp myimage, Docker creates a container named myapp. When the container stops (exits), it enters an "Exited" state ā but it's still there. The container, its filesystem, and its name all persist until you explicitly delete it with docker rm.
Most common scenario: you ran docker run --name myapp yesterday, the container stopped, and today you try to run it again. Docker sees that myapp already exists ā stopped or not ā and refuses to create a new container with the same name.
Step-by-Step Diagnosis
Step 1 ā List all containers including stopped ones
docker ps -a
Look for your container name in the NAMES column with Exited status.
Step 2 ā Filter by the specific name
docker ps -a --filter "name=myapp"
Step 3 ā Check logs before removing
docker logs myapp
Exited (0) = clean exit, higher = error. Check logs before removing if you need to debug.
Solutions
Solution 1 ā Remove the old container then re-run
docker stop myapp # Stop if somehow still running
docker rm myapp # Delete the container
docker run --name myapp ...
Solution 2 ā Force remove in one command
docker rm -f myapp
Solution 3 ā Use --rm for stateless containers
docker run --rm --name myapp myimage
--rm automatically deletes the container when it exits. Perfect for dev containers and one-off scripts.
Solution 4 ā Script for safe re-deployment
#!/bin/bash
docker rm -f myapp 2>/dev/null || true
docker run -d --name myapp --restart unless-stopped myimage:latest
Solution 5 ā Migrate to Docker Compose (permanent fix)
services:
myapp:
image: myimage:latest
restart: unless-stopped
docker compose up -d # Creates/recreates automatically
docker compose down # Removes containers cleanly