How to fix name is already in use by container error in Docker?

Better Stack Team
Updated on April 7, 2023

The error "name is already in use by container" occurs when you try to start a Docker container with a name that is already in use by another container. Here are some steps you can take to fix this error:

  1. List all the running and stopped containers on your system using the following command:

     
    docker ps -a
    

    This will give you a list of all containers and their names.

  2. Stop the container with the conflicting name using the following command:

     
    docker stop <container_name>
    

    Replace <container_name> with the name of the container that is using the name you want to use.

  3. Remove the container with the conflicting name using the following command:

    Note that if you remove a container, you will lose any changes made to the container's filesystem since it was created. If you want to preserve these changes, you can use the docker commit command to create a new image based on the container before removing it. This new image can then be used to create a new container with the desired name.

     
    docker rm <container_name>
    

    Replace <container_name> with the name of the container that is using the name you want to use.

  4. Start the container with the desired name using the following command:

     
    docker run --name <new_container_name> <image_name>
    

    Replace <new_container_name> with the name you want to use for the new container and <image_name> with the name of the Docker image you want to use to create the container.

    If you want to start the container using the same name as before, you can skip step 4 and simply start the container again with the original command you used to start it. This time, Docker will use the name you originally specified because the conflicting container has been removed.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack