Resolve a Docker container name conflict
Find the existing container, decide whether to restart, retain, or remove it, and avoid destructive cleanup hidden in deployment scripts.
Container names must be unique within a Docker daemon, including names held by stopped containers. “Name is already in use” usually means an existing container owns the requested name; it does not mean you need to delete an image.
Identify the owner
docker context show
docker ps -a --filter 'name=myapp'
docker container inspect myapp
The name filter can match more than one name, so inspect the exact intended container before acting. Confirm its ID, state, image, mounts, and deployment labels. If it is the service you meant to start again, docker start myapp may be the correct operation; docker run creates another container.
Choose one resolution
If the old stopped container is no longer needed, preserve required data and remove it:
docker rm myapp
If it must be retained, rename it to an unused name:
docker rename myapp myapp-retained
These are alternatives. Renaming a running container frees the name but does not free its published ports, stop writes to shared storage, or remove its other resource claims. Plan those conflicts before creating a replacement.
For a running container that should be removed, stop it deliberately before removal. docker rm -f terminates it abruptly and is not a harmless name-release operation.
Preserve the right data
docker diff helps identify writable-layer changes, but is not a complete application backup. Use docker cp for suitable files or the application's supported backup procedure for structured data. A commit snapshot excludes mounted volume contents.
Normal container removal does not remove named volumes or bind-mounted host data. Anonymous-volume removal options and --rm have additional lifecycle effects. Retaining storage is different from proving its data is consistent or that a new container will select the same storage.
Prevent routine CI conflicts
For genuinely disposable jobs, docker run --rm IMAGE COMMAND removes the container on exit. It does not resolve a name already taken before startup, and jobs still need a plan for logs, outputs, anonymous volumes, and interrupted execution.
Other options include Docker-generated names, unique validated job names, or distinct Compose project names. Avoid deployment scripts that silently run docker rm -f myapp || true before every start: they can destroy the wrong service and conceal an unrelated daemon or permissions failure.
FAQ
Why does docker ps show nothing?
It lists running containers by default. Use docker ps -a to include stopped ones.
What causes Compose conflicts?
A fixed container_name, shared project identity, or manually created container may occupy the expected name. Inspect Compose labels and the deployment source before removing it. Different projects are not isolated from name collisions when they deliberately use the same fixed container name.
Can Docker overwrite the existing container?
There is no general run option that safely replaces an arbitrary container by name. Reuse, rename, or remove the existing container as an explicit lifecycle decision.
See container run, container rename, and container rm.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan