Docker

How to Fix the “Name Already in Use by Container” Error

Docker is a well-known and extensively used project development platform that employs the containerization idea through the usage of containers. Docker containers are an important part of Docker. It allows users to design, manage, and deploy applications easily. It stores the entire project source code, dependencies, needed packages, and framework in containers. Therefore, the user may quickly deploy and share the application on another system.

Besides this, sometimes developers need some help during project or container creation and may face some conflicts, such as <container-name> is already in use by the container, or Docker daemon not working, and many more.

This blog will demonstrate how to fix a “name is already in use by container” error.

How to Fix the “Name Already in Use by Container” Error?

When a user creates a container, they may use the same container name as another container that already exists, or the container is created automatically during image creation. In such cases, the developer may encounter an “<container-name> is already in use by container” issue.

Follow the steps below to resolve the discussed error.

Step 1: Create Docker Container Through Docker Image

First, create a new container by utilizing the “docker create” command:

$ docker create --name python-container pythonimage

Here, you can see we get the error message that “/python-container” is already in use by the container:

Step 2: List Down All Containers

Let’s check if the container already exists with the same name or not. For this purpose, list down all Docker containers through the “docker ps” command. The option “-a” is used to view all containers:

$ docker ps -a

The output below shows that we already have a container with the same name as the one we are creating:

Step 3: Rename the Existing Container

To rename the existing container, utilize the “docker rename <old-name> <new-name>” command:

$ docker rename python-container pythoncontainer

Check again if the container was renamed by viewing the container list:

$ docker ps -a

We have successfully renamed the Docker container as “pythoncontainer”:

Step 4: Create Container

Now, create a new container by utilizing the below-provided command:

$ docker create --name python-container pythonimage

The output indicates that we have resolved the error and a new container is created:

Step 5: Start Container

In order to run/ start the container, execute the “docker start” command. Here, the “-i” option is added to execute the container in an interactive way:

$ docker start -i python-container

We have taught you how to fix the “name already in use by container” error.

Conclusion

To fix the “name already in use by container” Docker error, first, list all containers to verify if any container already exists with the same name. If yes, the developer can delete or rename the container to resolve the error. In order to rename the container, utilize the “docker rename <container-old-name> <container-new-name>” command. Then, try to recreate a container and verify if the issue is fixed. This blog has elaborated on how to fix the “name already in use by container” error.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.