Docker is a great tool to for software development and delivery. It enables you to separate applications from your infrastructure and deliver them softly and quickly.
How to remove Docker images?
Sometimes when we keep creating Docker images on our machine, or even on some cloud running Docker Container it'll soon start to run out of space. In this short post I'd like to share how I get rid of unnecessary Docker images.
The first step is to check the present images on your machine with this command:
$ docker images
The output should be like this:
REPOSITORY TAG IMAGE ID
mysql 5.7 b598110d0fff
mysql latest ed1ffcb5eff3
<none> <none> e81ad38baa90
redis latest dcf9ec9265e0
ruby 2.6.5-alpine 3304101ccbe9
postgres latest f9b3d2f9a593
mongo latest 58477a771fb4
rabbitmq latest 86a895bb41d6
redis 5.0.3-alpine 3d2a373f46ae
The output shows us the repository, it's the name at Docker Hub repository for the image. The TAG is the version of the image and the Image ID is the ID generated by Docker when we pull an image for our machine.
Before remove the desired image, it's important to know that the image is possibly associated with one or more containers. To check what containers are associated with images use the command below:
$ docker ps -a
The output will show us the ID of the container in our machine, the ID of an image we pulled and the entry command - this is the command to run our dockerized application.
CONTAINER ID IMAGE COMMAND
b8b33f28e089 ffb1b540cc8f "/bin/sh -c 'apt-get…"
e2c8159a0ee6 ffb1b540cc8f "/bin/sh -c 'apt-get…"
8d0910845ea5 ec6c8a057b83 "/bin/sh -c 'apt-get…"
c977dd983867 ec6c8a057b83 "/bin/sh -c 'apt-get…"
94f9fc8e6186 929b84501e14 "/bin/sh -c 'bundle …"
8d5de5c2c7a3 61f362c8bfb9 "/bin/sh -c 'bundle …"
3e3972e6aa86 15cc0f691e7b "/bin/sh -c 'bundle …"
f3175c20bad5 ed8edff8b2e2 "/bin/sh -c 'bundle …"
f76d4f777146 ed8edff8b2e2 "/bin/sh -c 'bundle …"
4eed76085da4 postgres "docker-entrypoint.s…"
505fc40d64c0 e81ad38baa90 "/bin/sh -c 'bundle …"
1804e73a047a redis "docker-entrypoint.s…"
3241275444f3 postgres "docker-entrypoint.s…"
154e700f0e65 postgres "docker-entrypoint.s…"
28bfc42fdac6 mongo:latest "docker-entrypoint.s…"
2e15f116cbf2 postgres "docker-entrypoint.s…"
6ca6f9bd1f1b rabbitmq:latest "docker-entrypoint.s…"
To remove the container and the image we will use two commands:
$ docker rm <container_id>
$ docker rmi <image_id>
We can use the -f
option for both commands to force the removal of a resource.
Here are the commands I use to remove all images and containers when I need to clean up my laptop.
$ docker ps -aq | xargs docker rm -f
$ docker images -aq | xargs docker rmi -f
Let's break it down. The docker ps
is the command to show the running containers, the option -a
will show all containers (even not running ones) and the -q
option will output only the CONTAINER ID.
The xargs
command is to build and execute a command from standard input. In our case we are passing to xargs
the container id from the docker ps -aq
command and the image id from the docker images -aq
command. So, the xargs
will build the exact command we need to remove the docker containers and images for each given ID we've got.
Removing Dangling Images
Dangling image means that a new build of the image was created, but wasn't given a name (untaged image). So, the old image becomes dangling. They'll be displayed as when you run docker images
If we need to remove only the dangling images, the command bellow works very fine. Add the option -a
to also remove unused images.
$ docker images prune
So, hope this might be useful for you as it is for me.
Thanks for reading.
Top comments (1)
docker (podman in my case...) system prune
does always a gooood cleaning work!