Docker

How to remove docker images

Docker images are nothing but a set of read-only files that means once the docker image is built, it can not be modified. However, you can create a new docker image with the help of the existing docker image. Docker images are used to build a docker container. Docker images come with multiple layers that are used to run code within a container. During the development process, many unused and outdated docker images are kept on the server until you manually remove it.

So it is necessary to remove the unused docker image from your system to free up the disk space.

In this tutorial, we will show you how to remove the docker images with the command-line.

Remove Docker Image

To remove the docker image from your system, you will need to list out all available images in your system.

You can list them using the following command:

docker image ls

You should see the following output:

REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 4bb46517cac3 2 days ago 133MB
wordpress latest f1da35a7ddca 3 days ago 546MB
mariadb latest b95867b52886 4 days ago 407MB

Now, you have a list of all images in your system. Next, locate the ID of the image that you want to remove and run the following command:

docker image rm 4bb46517cac3

You should see the following error:

Error response from daemon: conflict: unable to delete 4bb46517cac3 (cannot be forced)
- image is being used by running container 8f3d538370e5

The above output indicates any container uses the image you want to remove. So you will need to remove that container before removing the image.

If you want to remove multiple images, you will need to specify the ID of each docker image with “docker image rm” command:

docker image rm ID1 ID2 ID3

Remove Dangling Docker Images

A dangling image is an unused image that is not used by any container. You can remove the dangling image from your system by running the following command:

docker image prune

You will be prompt to type y to continue, as shown below:

WARNING! This will remove all dangling images.

Are you sure you want to continue? [y/N] y

Total reclaimed space: 0B

Remove All Unused Docker Images

Docker allows you to remove all images that are not used by any containers using a single command, as shown below:

docker image prune -a

You will be prompt to type y to continue, as shown below:

WARNING! This will remove all images without at least one container associated with them.

Are you sure you want to continue? [y/N] y

Deleted Images:
untagged: ubuntu:latest
untagged: [email protected]:5d1d5407f353843ecf8b16524bc5565aa332e9e6a1297c73a92d3e754b8a636d
deleted: sha256:1e4467b07108685c38297025797890f0492c4ec509212e2e4b4822d367fe6bc8
Total reclaimed space: 0B

Conclusion

In this guide, we have shown how to remove docker images in several ways. For more information, check out the Docker official documentation.

About the author

Hitesh Jethva

Hitesh Jethva

I am Hitesh Jethva lives in Ahmedabad, INDIA. I am Linux system administrator and Technical writer.

I felt in love with Linux while i was started to learn Linux. I am a fan of open source technology and have more than 8+ years of experience in Linux and Open Source technologies. My main motto is to make uncomplicated things easier.

I have extensive experience within the following areas: CentOS/RHEL/FreeBSD/Ubuntu/Debian, cPanel/WHM, High Availability Architecture, Proactive security/Web Application Security, Amazon Web Services (AWS), Apache/Nginx, OpenLDAP, DevOps automation: Chef/Docker.

My expertise includes Linux system administration, installation, configuration, tuning, security and troubleshooting. I have large skills in configuring/management/support of Apache, Nginx, MariaDB, MongoDB, PHP-FPM, Nagios, Zabbix, Graylg, Redis, Docker and much more.