How to delete Docker images

Still plagued by my undersized root / partition, I’ve had to learn a thing or
two about freeing up disk space. Recently, after a string of adventures with
containers, I came to realize that Docker stores it’s images in the root
partition, by default at least.

To remove the images I have stored locally, and free up disk space, we first
need to list out the images we have downloaded, which will look something like
this:

% docker images
REPOSITORY   TAG          IMAGE ID       CREATED        SIZE
mysql        latest       91b53e2624b4   6 weeks ago    565MB
adminer      latest       8e4345c1c3dc   6 weeks ago    250MB
mysql        8            8189e588b0e8   3 months ago   564MB
ubuntu       20.04        88bd68917189   3 months ago   72.8MB
php          7.4-apache   8b6f3afc00ee   3 months ago   798MB
mono         6            af8a21454b9a   7 months ago   788MB
mono         latest       af8a21454b9a   7 months ago   788MB
debian       10           53906583a002   7 months ago   114MB
debian       latest       446440c01886   7 months ago   124MB
mongo        4.2          df2171176868   7 months ago   388MB
ubuntu       22.04        6b7dfa7e8fdb   7 months ago   77.8MB
mysql        5.7          d410f4167eea   7 months ago   495MB
redis        5.0          99ee9af2b6b1   8 months ago   110MB
php          <none>       20a3732f422b   8 months ago   453MB
Zsh

To remove an image, you can then use the following command:

% docker rmi NAMEOFIMAGE
Zsh

Not much to it, but it doesn’t always go according to plan. If the image you’d
like to remove is still tied to a container, you’ll receive a message like this:

% docker rmi adminer
Error response from daemon: conflict: unable to remove repository reference "adminer" (must force) - container b73f553d6fc1 is using its referenced image 8e4345c1c3dc
Zsh

Never fear though, all you have to do is remove the container that’s blocking
things:

% docker rm b73f553d6fc1
b73f553d6fc1
Zsh

And then, simply try again:

% docker rmi adminer
Untagged: adminer:latest
Untagged: adminer@sha256:bb57616441db7d9bcf04f9e231bf8fd066b209374f0e30384d17988cbb4e889b
Deleted: sha256:8e4345c1c3dc89b4c31146abe340fa39e143d922d1e3401233d7ac32db2d985d
Deleted: sha256:e0e5293a8dac2c93826e3cc883686cbef1b587f34ad1d9ea0e94f1bf7affeb78
Deleted: sha256:9ddc8930c09d6d725484993e70deb3d5a8fb9ca0b4ecd385cc4d93174b869579
Deleted: sha256:fede545bed63aeacf77d6e41a73d8a362eb49227d5fe39d0f447723eace99cbb
Deleted: sha256:ee5f8184868400dbcce39cd4918c3d0d91da3807cb37c32b2260eda852d2c8e1
Deleted: sha256:b7c40e2d7d736aa7880fcf4ebe6ee0c075a4ed16cec4c80592b0abcaee082664
Deleted: sha256:d96b1a9770a32366607324d4657d0fea0aee738e227e8941aab59a53754508d4
Deleted: sha256:c28dd92cbb2619b4dc09bf09ef075bf5eedb4e471ff21a109fcb05437e2c0c7b
Zsh

You may have to remove containers a few times before you can remove the image complete, so repeat as necessary!

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.