While creating or executing a Docker container, the beginner may face the error “Name already in use by container” problem. This error may confuse or frustrate the new user. You can fix the stated problem by either restarting the Docker container, renaming the existing containers, removing the old container, or by changing the container name.

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

Docker containers are used to develop, deploy, and deliver applications or software in small packages. Containers are a key entity of the Docker development environment that encapsulates the application program, essential configurations, required packages, and dependencies.

Occasionally while creating the container through the “docker create” command or executing the container through the “docker run” command, the user may face the error “<cont-name> is already in use by container” as shown below:

The container name is already in use by container

The stated error occurs when the user tries to restart or execute the container through the “docker run” command. The “docker run” command not only starts the container but also creates the container from scratch by reading the Docker image.

So, when the user tries to execute the container that already existed through the “docker run” command, this will try to recreate the container with the specified name and a conflict will occur that shows a “Name already in use by container” error.

This case is the same happens in the “docker create” command. The “docker create” command is utilized to only create the container. When a user tries to create the container with the name that is already used by any other container, they encounter the stated conflict.

To fix the stated conflict, try out the following solutions:

  • Restart Container
  • Rename Container
  • Remove Container
  • Change Container Name

Solution 1: Restart Container

When users try to restart the container through the “docker run” command, they encounter the stated problem. If you want to execute the same image in the container, you can only restart the existing container without creating a new container. For this purpose, simply use the “docker restart <cont-name>” command:

docker restart py-container

This command will restart the existing container that you want to start through the “docker run” command:

docker restart py-container

Solution 2: Rename Container

In case, if users do not want to use the existing container but recreate the container from scratch with the same name that is already used by another container. In this case, the user can create a new container by renaming the existing container.

Let’s take an example: the “py-container” already exists in the Docker host. However, the user wants to generate a new container with the same “py-container” name. In this situation, you need to first rename the existing container and then create the new container. For demonstration, follow the below steps.

Step 1: Rename the Container

First rename the existing container through the “docker rename <cont-name> <new-cont-name>” command:

docker rename py-container python-container

Here, we have replaced the name of “py-container” with “python-container”:

docker rename py-container python-container

Step 2: View Docker Containers

For verification, list down the containers through the below command. Here, the “-a” flag is used to display all stopped and executing containers:

docker ps -a

The output indicates that we have successfully renamed the Docker container:

docker ps -a

Step 3: Create the Docker Container

Now, create the new container with the “py-container” name through the “docker run” command:

docker run --name py-container py-img

Here, “–name” is utilized to specify the name of the container. The output shows that we have effectively resolved the stated conflict and created the new container with the “py-container” name:

docker run --name py-container py-img

Again, list down the container and check if the container is created or not:

docker ps -a
docker ps -a

Solution 3: Remove Container

Another possible way to resolve the “Name is already in use” problem in Docker is to remove the existing container and recreate the new container either using the “docker run” or “docker create” command.

For this purpose, first, remove the existing container through the “docker rm <container-name>” command:

docker rm py-container
docker rm py-container

After that, create and start the new container through the below command:

docker run --name py-container py-img

Here, you can see we have effectively created and executed the “py-container” without any conflict or error:

docker run --name py-container py-img

Solution 4: Change Container Name

To fix the “name already is in use by container” problem, one of the simplest solutions is to create the container with another name. Such as “py-container” can be replaced by “py-container1”. This will never affect the working of running applications inside the container. 

In the below command, we simply create the new container by minute changing in container name:

docker run --name py-container1 py-img

This will also fix the stated issue as shown below:

docker run --name py-container1 py-img

We have explained the fixes to resolve the “<cont-name> is already in use by container” error.

Conclusion

The error “name already is in use by container” occurs if the user tries to create the container with the name that is already occupied by another container. To fix the stated issue, the user can either remove the existing Docker container and recreate the container, rename the existing container, and create a new one, or simply change the container name in the “–name” option of the “docker run” command. This write-up has discussed the fixes to tackle the “<cont-name> is already in use by container” error.