Docker Error: “Name is already in use by container” – How to Fix It

When working with Docker, you may encounter this common error:

Error response from daemon: Conflict. The container name "/your-container-name" is already in use by container

This typically happens when you try to run a new container using a name that’s already assigned to an existing (or stopped) container.

In this blog post, you’ll learn:

  • Why this error occurs
  • How to find and remove the conflicting container
  • How to avoid the issue in future

❓ Why This Error Happens

Docker requires container names to be unique. When you try to start a container with a name that already exists (even for a stopped container), you’ll get the error:

docker run --name myapp myimage

🔴 Output:

docker: Error response from daemon: Conflict. The container name "/myapp" is already in use by container ...

This means there’s already a container named myapp, possibly created earlier and not removed.


✅ Solution 1: Remove the Old Container

First, check if the container exists—even in a stopped state:

docker ps -a

If you find the container with the same name, remove it:

docker rm myapp

Then, retry your docker run command.


✅ Solution 2: Rename the Old Container

If you want to keep the old container but avoid the name conflict, rename it:

docker rename myapp myapp_old

Now you’re free to use myapp again for a new container.


✅ Solution 3: Use a Different Name

Alternatively, assign a new name to the container:

docker run --name myapp_v2 myimage

This avoids touching existing containers.


🧼 Pro Tip: Clean Up Automatically

To ensure containers don’t linger around unintentionally, use the --rm flag:

docker run --rm --name myapp myimage

This removes the container once it exits, freeing up the name automatically.


🔍 Summary

TaskCommand
List all containersdocker ps -a
Remove a stopped containerdocker rm <name>
Rename a containerdocker rename old new
Use different container namedocker run --name newname
Auto-remove container on exitdocker run --rm

📝 Conclusion

The error “Name is already in use by container” is Docker’s way of preventing name collisions. By removing or renaming old containers—or using unique names—you can easily avoid this issue.

Sharing Is Caring:

Leave a Comment