I noticed that Docker images were starting to pile up on my Windows 10 Pro workstation from tutorials that I had done in the past. I wondered, “Where is the image stored locally?“, so I Googled it. Apparently the images are stored inside the Moby Linux VM that is used by Docker for Windows. You can see that VM running in the Hyper-V Manager window when Docker is running:
If I run the “docker images” command, I can see what images are in my local repository.
The highlighted image is one that I’m actively working on. Some of the others no longer serve any purpose, so I’d like to dispose of them. Now that I know they are stored in the Moby Linux VM that powers Docker locally, I don’t want that space being used. That VM space is allocated from my primary SSD drive, so space is at a premium. How can I get rid of old images that I no longer want to keep?
A quick Google search found this Docker reference page:
Seems easy enough. Just use the command:
docker image rm <imagename>
I’ll start by removing an image from a tutorial that I did months ago.
Uh oh… The result was an error message:
Error response from daemon: conflict: unable to remove repository reference “springboothelloworld” (must force) – container 1e6b7a61c240 is using its referenced image 0ba120ff1e58
It hadn’t occurred to me that Docker keeps a snapshot of the container used to run the image, as well as the image itself. I could probably use the ‘force’ option as the error message states, but I’d rather take this opportunity to learn the proper way to clean up old no-longer-running container instances. Let’s see what I have hanging around on my workstation:
docker ps -a
This list of old container instances isn’t a surprise. I saw these when I was working on my Spring Boot image tutorial recently. It didn’t occur to me that these old containers were taking up space, and at the time I thought the ‘docker ps -a‘ command was intended to display a sort of run history. Well now I know better, but how do I clean up these old containers?
Removing Docker container instances
The link above provides some nice examples of how to do this. Unfortunately, their more advanced examples use Linux shell script magic that won’t work on Windows. I’ll have to stick with the simple one-at-a-time removal commands.
Let’s start with this:
docker rm nifty_keller
It should remove only the container instance with the name ‘nifty_keller‘.
The ‘docker rm’ command seems to respond with the name of the container that it removed. Everything looks correct when I issue the ‘docker ps -a’ command again, and ‘nifty_keller’ is now gone from the list.
I have no use for any of these old containers, so I’ll remove each of them.
So how can I avoid creating this mess of dead container instances in the future?
docker run --rm <image_to_run>
Adding the ‘–rm’ argument to the ‘docker run’ command will cause the container to be automatically removed after the run terminates. I’m still too new to Docker to know what benefits there are to reusing a container instance. At this stage, when I’m still messing around with tutorials and simple test apps, there is no need to keep the containers.
Removing Docker image instances
Now I’ll make another attempt at deleting some of those old images.
I used this command:
docker image rm springboothelloworld
It removed the image named ‘springboothelloworld’, and apparently the ‘latest’ tag for that image name. But why does it show four image ids being deleted rather than just one? I can’t answer that yet. I’ll hazard a guess that I made some minor changes while I was doing that tutorial, and each change/build generated a new image that was built on top of the previous one. For now, I’m not going to worry about it. When I’m doing tutorial projects I only care about the ‘latest’ version of my images.
Now I’ll get rid of some more unwanted images.
Well I just learned something else. If the image has a tag other than ‘latest’, you just specify the tag name when attempting to remove the image. I tried to remove an image that had tag name ‘part1’, but I didn’t specify the tag name. I got the error message ‘No such image’. When I used the following command, the remove was successful:
docker image rm jtough/get-started:part1
So the format of the command is:
docker image rm <image_name>:<tag_name>
After cleaning up the images I don’t want, I was left with this:
Note the highlighted entry. It has no name and no tag name. I’ll guess that it is related to the ‘openjdk’ base image that was created around the same time. Weird, but I’m not going to worry about it right now.