Removing old Docker images and containers

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:

DOAWS-screenshot-004
Hyper-V Manager in Windows 10 Pro

If I run the “docker images” command, I can see what images are in my local repository.

DOAWS-screenshot-005

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:

https://docs.docker.com/engine/reference/commandline/image_rm/

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.

DOAWS-screenshot-006

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

Interesting.

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

DOAWS-screenshot-007

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

https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes

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‘.

DOAWS-screenshot-008

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.

DOAWS-screenshot-009

All gone.

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.

DOAWS-screenshot-010

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.

DOAWS-screenshot-011

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:

DOAWS-screenshot-012

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.


 

Leave a Reply