{"id":11805,"date":"2019-03-17T11:41:04","date_gmt":"2019-03-17T11:41:04","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11805"},"modified":"2019-03-17T11:41:04","modified_gmt":"2019-03-17T11:41:04","slug":"how-to-remove-docker-images-containers-and-volumes-2","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/17\/how-to-remove-docker-images-containers-and-volumes-2\/","title":{"rendered":"How to Remove Docker Images, Containers and Volumes"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/blog.docker.com\/wp-content\/uploads\/illus5-colorful-square.jpg\" alt=\"Related image\" \/><\/p>\n<p><strong>Docker<\/strong>\u00a0is 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.<\/p>\n<p>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.<\/p>\n<p>We have already covered a series on\u00a0<strong>Docker<\/strong>, that explains how to install Docker, run applications into containers and automatically build docker images with dockerfile.<\/p>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/install-docker-and-learn-containers-in-centos-rhel-7-6\/\" target=\"_blank\" rel=\"noopener\">Install Docker and Learn Basic Container Manipulation in CentOS and RHEL 7\/6<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/install-run-and-delete-applications-inside-docker-containers\/\" target=\"_blank\" rel=\"noopener\">How to Deploy and Run Applications into Docker Containers on CentOS\/RHEL 7\/6<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/build-and-configure-docker-container-images-with-dockerfile\/\" target=\"_blank\" rel=\"noopener\">Automatically Build and Configure Docker Images with Dockerfile on CentOS\/RHEL 7\/6<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/install-apache-web-server-in-a-docker-container\/\" target=\"_blank\" rel=\"noopener\">How to Setup a Simple Apache Web Server in a Docker Container<\/a><\/li>\n<\/ol>\n<p>In this article, we will explain how to remove docker images, containers and volumes via the docker command line tool in Linux systems.<\/p>\n<h3>How to Remove Docker Images<\/h3>\n<p>Before you remove any docker images, you can list all existing images on your system with the image management command.<\/p>\n<pre>$ docker image\t        #list the most recently created images\r\nOR\r\n$ docker image -a \t#list all images\r\n<\/pre>\n<p>Looking at the output in the screenshot that follows, we have some images without a tag (showing\u00a0<code><\/code>\u00a0instead), these are referred to as \u201c<strong>dangling images<\/strong>\u201d. They no longer have any relationship to any tagged images; they are not useful anymore and only consume disk space.<\/p>\n<div id=\"attachment_29864\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/list-docker-images.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-29864\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/list-docker-images.png\" sizes=\"auto, (max-width: 892px) 100vw, 892px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/list-docker-images.png 892w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/list-docker-images-768x378.png 768w\" alt=\"List Docker Images\" width=\"892\" height=\"439\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">List Docker Images<\/p>\n<\/div>\n<p>You can remove one or more old or unused Docker images using the image\u00a0<strong>ID<\/strong>, for example (where\u00a0<strong>d65c4d6a3580<\/strong>\u00a0is the image ID).<\/p>\n<pre>$ docker rmi d65c4d6a3580 \t\t\t\t#remove a single image\r\n$ docker rmi 612866ff4869 e19e33310e49 abe0cd4b2ebc\t#remove multiple images\r\n<\/pre>\n<p>You can list dangling images (untagged images) using the\u00a0<code>-f<\/code>\u00a0filter flag as shown.<\/p>\n<pre>$ docker images -f dangling=true\t\r\n<\/pre>\n<div id=\"attachment_29865\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/list-docker-dangling-images.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-29865\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/list-docker-dangling-images.png\" sizes=\"auto, (max-width: 892px) 100vw, 892px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/list-docker-dangling-images.png 892w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/list-docker-dangling-images-768x263.png 768w\" alt=\"List Dangling Docker Images\" width=\"892\" height=\"306\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">List Dangling Docker Images<\/p>\n<\/div>\n<p>To remove all dangling images, allowing you to reclaim wasted disk space, use any of these commands.<\/p>\n<pre>$ docker image prune\t\t#interactively remove dangling images\r\nOR\r\n$ docker rmi $(docker images -q -f dangling=true)\r\n<\/pre>\n<div id=\"attachment_29866\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/remove-all-dangling-images.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-29866\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/remove-all-dangling-images.png\" alt=\"Remove All Dangling Images\" width=\"714\" height=\"587\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Remove All Dangling Images<\/p>\n<\/div>\n<p>To remove all not associated with any container, use the following command.<\/p>\n<pre>$ docker image prune -a \t\r\n<\/pre>\n<h3>How to Remove Docker Containers<\/h3>\n<p>You can start by listing all docker containers on your system using following command.<\/p>\n<pre>$ docker ps\r\nOR\r\n$ docker ps -a  \r\n<\/pre>\n<div id=\"attachment_29867\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/list-all-containers.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-29867\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/list-all-containers.png\" sizes=\"auto, (max-width: 1026px) 100vw, 1026px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/list-all-containers.png 1026w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/06\/list-all-containers-768x80.png 768w\" alt=\"List Docker Containers\" width=\"1026\" height=\"107\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">List Docker Containers<\/p>\n<\/div>\n<p>Once you have identified the container (s) you want to delete, you can remove them using their ID, for example.<\/p>\n<pre>$ docker rm 0fd99ee0cb61\t\t#remove a single container\r\n$ docker rm 0fd99ee0cb61 0fd99ee0cb61   #remove multiple containers\r\n<\/pre>\n<p>If a container is running, you can first stop it and remove it as shown.<\/p>\n<pre>$ docker stop 0fd99ee0cb61\r\n$ docker rm -f 0fd99ee0cb61\r\n<\/pre>\n<p>You can also\u00a0<strong>force-remove<\/strong>\u00a0a container while it is running by adding the\u00a0<code>--force<\/code>\u00a0or\u00a0<code>-f<\/code>\u00a0flag, this will send it a\u00a0<strong>SIGKILL<\/strong>\u00a0signal as shown.<\/p>\n<pre>$ docker rm -f 0fd99ee0cb61\r\n<\/pre>\n<p>You can remove containers using filters as well. For example to remove all exited containers, use this command.<\/p>\n<pre>$ docker rm $(docker ps -qa --filter \"status=exited\")\r\n<\/pre>\n<p>To stop and remove all containers, use the following commands.<\/p>\n<pre>$ docker stop $(docker ps -a -q)\t#stop all containers\r\n$ docker container prune\t\t#interactively remove all stopped containers\r\nOR\r\n$ docker rm $(docker ps -qa)\r\n<\/pre>\n<h3>How To Remove Docker Volumes<\/h3>\n<p>As before, begin by listing all docker volumes on your system with the volume management command as shown.<\/p>\n<pre>$ docker volume ls\r\n<\/pre>\n<p>To remove one or more volumes, use the following command (note that you can\u2019t remove a volume that is in use by a container).<\/p>\n<pre>$ docker volume rm volume_ID \t           #remove a single volume \r\n$ docker volume rm volume_ID1 volume_ID2   #remove multiple volumes\r\n<\/pre>\n<p>Use the\u00a0<code>-f<\/code>\u00a0flag to force the removal of one or more volumes.<\/p>\n<pre>$ docker volume rm -f volume_ID\r\n<\/pre>\n<p>To remove dangling volumes, use the following command.<\/p>\n<pre>$ docker volume rm $(docker volume ls  -q --filter dangling=true)\r\n<\/pre>\n<p>To remove all unused local volumes, run the following command. This will remove volumes interactively.<\/p>\n<pre>$ docker volume prune\t\r\n<\/pre>\n<h3>How to Remove Unused or Dangling Images, Containers, Volumes, and Networks<\/h3>\n<p>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.<\/p>\n<pre>$ docker system prune\r\n<\/pre>\n<p>To prune volumes, simply add the\u00a0<code>--volumes<\/code>\u00a0flag to the below command as shown.<\/p>\n<pre>$ docker system prune --volumes\r\n<\/pre>\n<p><strong>Note<\/strong>: In order to run the docker command line tool without the\u00a0<a href=\"https:\/\/www.tecmint.com\/su-vs-sudo-and-how-to-configure-sudo-in-linux\/\" target=\"_blank\" rel=\"noopener\">sudo command<\/a>, you need to add a user to docker group, for instance.<\/p>\n<pre>$ sudo usermod -a -G docker aaronkilik\r\n<\/pre>\n<p>For more information, see the help page for the above docker object management commands.<\/p>\n<pre>$ docker help\r\n$ docker image help   \r\n$ docker container help   \r\n$ docker volume help   \r\n<\/pre>\n<p>That\u2019s 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.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/remove-docker-images-containers-and-volumes\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Docker\u00a0is 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 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/17\/how-to-remove-docker-images-containers-and-volumes-2\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Remove Docker Images, Containers and Volumes&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11805","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11805","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11805"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11805\/revisions"}],"predecessor-version":[{"id":11806,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11805\/revisions\/11806"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}