Skip to content

Fix: Docker container name already in use

FixDevs ·

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 use

Docker 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 stop does 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-app twice without removing the first.
  • Script or CI/CD pipeline rerun. A deployment script runs docker run --name on 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-app

If the container is still running, stop it first:

docker stop my-app
docker rm my-app

Or force remove a running container:

docker rm -f my-app

Then run your new container:

docker run --name my-app my-image

Pro Tip: Use docker rm -f in scripts and CI/CD pipelines. It removes the container whether it is running, stopped, or already removed (the -f flag 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-image

This 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 -d

docker-compose down stops and removes containers, networks, and default volumes. This ensures a clean state.

Remove orphaned containers:

docker-compose down --remove-orphans

If 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 -d

Force recreate containers:

docker-compose up -d --force-recreate

This rebuilds containers even if their configuration has not changed.

Common Mistake: Using docker-compose stop instead of docker-compose down between deployments. stop only stops containers — it does not remove them. The next up finds the old containers and tries to start them rather than creating new ones. Use down for 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-image

With random suffixes:

docker run --name "my-app-$(head /dev/urandom | tr -dc a-z0-9 | head -c 6)" my-image

Without 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-image

Or use docker run --replace (Docker 25.0+):

docker run --replace --name my-app -d my-image

The --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 prune

Or with force (no confirmation prompt):

docker container prune -f

Remove 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 name

Restart policies:

PolicyBehavior
noNever restart (default)
alwaysAlways restart, including after Docker daemon restart
unless-stoppedRestart unless manually stopped
on-failureRestart 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-image

Later, inspect the old container:

docker logs my-app-old
docker inspect my-app-old

And remove it when done:

docker rm my-app-old

Still 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 default

Check 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 docker

Check 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.

F

FixDevs

Solo developer based in Japan. Every solution is cross-referenced with official documentation and tested before publishing.

Was this article helpful?

Related Articles