Jun's Blog

Output, activities, memo and etc.

How to remove not removed container images & useful docker commands

Before next blog for qemu-user-static. I would write some articles.

How to remove not removed container images

The subject is that I faced.

The reason is after upgrading a docker, the actual storage to manage containers were changed. Then docker image ls command could still see the containers. But docker rmi or docker system prune can not detect the containers to remove it internally.

  • A: A docker old version: actual storage is at /var/lib/docker/devicemapper
  • B: A docker new version: actual storage is at /var/lib/docker/overlay2.

The steps I did is 1. Remove "A" rpm packages of the old version. Then /var/lib/docker was kept not deleted. 2. Install "B" rpm packages of the new version.

So, when I faced this situation, what I did is blew steps.

$ sudo systemctl stop docker

$ sudo rm -rf /var/lib/docker

$ sudo systemctl start docker

When run systemctl start docker, the new /var/lib/docker is created in the process.

Useful docker commands

docker image ls -a

To show all the container images including intermediate images.

docker system prune -f -a --volumes

It is useful to remove all the container image. The created images often occupy your hard disk.

$ docker system prune --help

Usage:  docker system prune [OPTIONS]

Remove unused data

Options:
  -a, --all             Remove all unused images not just dangling ones
      --filter filter   Provide filter values (e.g. 'label=<key>=<value>')
  -f, --force           Do not prompt for confirmation
      --volumes         Prune volumes

docker info

Useful information such as a storage driver and registry.

docker version

To know the version.

docker image build --rm -t sample/foo .

image can be ommited. But I prefer to show it explicitly. When building, adding --rm is very important to reduce the container image size. But it seems the default is true for my docker.

$ docker image build --help
...
      --rm                      Remove intermediate containers after a successful build
                                (default true)

Adding namespace "sample" is important to distinguish current focusing domain.

docker container run --rm

In case to create a small container image. busybox is useful. container can be omitted. But I prefer to show it explicitly.

$ docker container run --rm -t busybox uname -a
Linux 29722d64ab62 5.0.5-200.fc29.x86_64 #1 SMP Wed Mar 27 20:58:04 UTC 2019 x86_64 GNU/Linux
$ docker container run --help
...
      --rm                             Automatically remove the container when it exits
...