Error Encyclopedia

Container Name Is Already in Use (Docker)

Fix 'The container name is already in use by container' Docker error. Learn to remove, rename, or reuse existing containers.

What Does This Error Mean?

Docker error 'The container name is already in use' means you are trying to create a container with a name that is already assigned to an existing container (running or stopped). Container names must be unique across all containers on a Docker host.

Common Causes

1

A container with the same name already exists (running or stopped)

2

Running docker run --name with the same name twice

3

docker-compose recreating a service without removing the old container

4

A previous run of the same command left a stopped container with that name

5

CI/CD pipeline creating containers without cleaning up

How to Fix It

Remove the existing container

Remove the container with the conflicting name before creating a new one.

# Remove the existing container (use -f if it is running)
docker rm container-name

# Force remove a running container
docker rm -f container-name

# Remove all stopped containers
docker container prune

# Verify the container is gone
docker ps -a | findstr container-name

Use --rm for ephemeral containers

The --rm flag automatically removes the container when it stops.

# Without --rm: container stays after stopping
docker run --name myapp myimage
# Next run: "name is already in use"

# With --rm: automatically removed after exit
docker run --rm --name myapp myimage
# Can run again immediately

Restart the existing container instead

If the existing container is stopped, restart it instead of creating a new one.

# Check if the container exists but is stopped
docker ps -a | grep container-name

# Start the existing container
docker start container-name

# Or docker-compose: recreate without name conflict
docker-compose up --force-recreate

Related Errors

Other common errors in this category:

Frequently Asked Questions

Can I rename a Docker container instead of removing it?

Yes. Use `docker rename old-name new-name` to rename an existing container without removing it. This preserves the container and its data while freeing the original name for a new container.

How do I avoid container name conflicts in CI/CD?

Use unique names (e.g., app-build-${CI_COMMIT_SHORT_SHA}), omit the --name flag to let Docker generate random names, or always run `docker rm -f container-name` before creating a new one.