Updated: September 2, 2015
Recently, I've written a few interesting tutorials on the use of Docker, a neat technology that wraps LXC in a furry bundle of joy, and lets, how shall I put, less autistically inclined developers, engineers and system administrators, not so keen on Python vomit, enjoy the wonders of OS-level virtualization. But there are problems.
The two issues we will discuss today occur when you start a named container, or try to delete one. Namely, the fancy letters on the screen will read something like: "Conflict. The name W is already in use by container XYZ. You have to delete..." And the second piece of trouble reads: "Conflict, cannot remove the default name of the container..." Apart from the blatant misuse of punctuation, we now need to figure out how to sort this out.
All right, so we have this issue:
docker run -d -ti -p 22 -p 80 --name <name> <repo:tag>
FATA Error response from daemon: Conflict. The name "XYZ" is already in use by container hexacode. You have to delete (or rename) that container to be able to reuse that name.
We cannot start a container, because the name is already in use. Run the docker ps command, you won't see it there. Moreover if you try to kill or remove the offending container leftovers, you get:
docker rm --link <name>
Error response from daemon: Conflict, cannot remove the default name of the container
FATA Error: failed to remove one or more containers
Seems like a dead end. So what now?
First, let's figure out what we have at hand. Apparently, at some point in the past, you have created a named container, and then, you have left it running. After that, your host was rebooted for whatever reason, and you have not gracefully terminated your containers. Leftover files now seem to prevent you from respawning new containers with the old name, because the system thinks the old ones are still in place.
There are two ways we can fix the issue. We will start with the graceful one first. If you run the docker ps command, you will not see your old containers, because they are not really running. But if you run the docker os command with the -a flag, you should see all of your container run history:
Containers that have exited in an abnormal fashion will have a non-zero status code. Search for the ones you need, based on their name, and them remove them using the actual hexadecimal code, e.g.:
docker rm 85c2981e63f6 26ea04f41ab3
The second method is to manually delete the leftover files from under /var. Specifically, the files can be found under /var/lib/docker/containers/. Each container directory will have a super long hash identifier, and inside it, tons of ugly JSON output. If you search for a particular string that matches your desired container name, you will find the relevant directory that holds its configuration files.
Delete the parent directory matching the name search, then restart the Docker service. After that, you should be able to use your name again. This is like what we've seen with KVM and libvirt leftovers.
systemctl restart docker
And you should be up and running, old boy!
Some problems can be really annoying, especially when they relate to how well and robustly services handle crashes, reboots and such. I dislike when leftover files cause so much grief. It should not be like that, and services ought to recover majestically. But hey, containers are still a young technology, so this kind of stuff is expected.
Hopefully, like a glorious aircon system blasting its fury on a windshield, we have demistified the fog and confusion around the creation of named containers and deletion of leftovers following unexpected reboots and whatnot. Now, your Docker experience will be a tad more pleasant, and you've learned a bit or two on how things work in the background. There you go, have fun.