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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github