Docker is an open-source, powerful, secure, reliable and efficient container platform that enables realistic independence between applications and infrastructure. It is being widely adopted by IT and cloud companies out there, to easily to create, deploy, and run applications.
A container is a technology for visualizing operating systems, that enables an application to be packaged with everything needed to run it, allowing it to run independently from the operating system. A container image is a self-contained, executable package of an application that includes everything needed to run it: code, runtime, system tools and libraries, as well as configurations.
We have already covered a series on Docker, that explains how to install Docker, run applications into containers and automatically build docker images with dockerfile.
- Install Docker and Learn Basic Container Manipulation in CentOS and RHEL 7/6
- How to Deploy and Run Applications into Docker Containers on CentOS/RHEL 7/6
- Automatically Build and Configure Docker Images with Dockerfile on CentOS/RHEL 7/6
- How to Setup a Simple Apache Web Server in a Docker Container
In this article, we will explain how to remove docker images, containers and volumes via the docker command line tool in Linux systems.
How to Remove Docker Images
Before you remove any docker images, you can list all existing images on your system with the image management command.
$ docker image #list the most recently created images OR $ docker image -a #list all images
Looking at the output in the screenshot that follows, we have some images without a tag (showing
instead), these are referred to as “dangling images”. They no longer have any relationship to any tagged images; they are not useful anymore and only consume disk space.
You can remove one or more old or unused Docker images using the image ID, for example (where d65c4d6a3580 is the image ID).
$ docker rmi d65c4d6a3580 #remove a single image $ docker rmi 612866ff4869 e19e33310e49 abe0cd4b2ebc #remove multiple images
You can list dangling images (untagged images) using the
-f filter flag as shown.
$ docker images -f dangling=true
To remove all dangling images, allowing you to reclaim wasted disk space, use any of these commands.
$ docker image prune #interactively remove dangling images OR $ docker rmi $(docker images -q -f dangling=true)
To remove all not associated with any container, use the following command.
$ docker image prune -a
How to Remove Docker Containers
You can start by listing all docker containers on your system using following command.
$ docker ps OR $ docker ps -a
Once you have identified the container (s) you want to delete, you can remove them using their ID, for example.
$ docker rm 0fd99ee0cb61 #remove a single container $ docker rm 0fd99ee0cb61 0fd99ee0cb61 #remove multiple containers
If a container is running, you can first stop it and remove it as shown.
$ docker stop 0fd99ee0cb61 $ docker rm -f 0fd99ee0cb61
You can also force-remove a container while it is running by adding the
-f flag, this will send it a SIGKILL signal as shown.
$ docker rm -f 0fd99ee0cb61
You can remove containers using filters as well. For example to remove all exited containers, use this command.
$ docker rm $(docker ps -qa --filter "status=exited")
To stop and remove all containers, use the following commands.
$ docker stop $(docker ps -a -q) #stop all containers $ docker container prune #interactively remove all stopped containers OR $ docker rm $(docker ps -qa)
How To Remove Docker Volumes
As before, begin by listing all docker volumes on your system with the volume management command as shown.
$ docker volume ls
To remove one or more volumes, use the following command (note that you can’t remove a volume that is in use by a container).
$ docker volume rm volume_ID #remove a single volume $ docker volume rm volume_ID1 volume_ID2 #remove multiple volumes
-f flag to force the removal of one or more volumes.
$ docker volume rm -f volume_ID
To remove dangling volumes, use the following command.
$ docker volume rm $(docker volume ls -q --filter dangling=true)
To remove all unused local volumes, run the following command. This will remove volumes interactively.
$ docker volume prune
How to Remove Unused or Dangling Images, Containers, Volumes, and Networks
You can delete all dangling and unreferenced data such as containers stopped, images without containers, with this single command. By default, volumes are not removed, to prevent vital data from being deleted if there is currently no container using the volume.
$ docker system prune
To prune volumes, simply add the
--volumes flag to the below command as shown.
$ docker system prune --volumes
Note: In order to run the docker command line tool without the sudo command, you need to add a user to docker group, for instance.
$ sudo usermod -a -G docker aaronkilik
For more information, see the help page for the above docker object management commands.
$ docker help $ docker image help $ docker container help $ docker volume help
That’s all for now! In this article, we have explained how to remove docker images, containers and volumes via the docker command line tool. If you have any questions or thoughts to share, use the feedback form below to reach us.