Removing Docker Containers and Images
In a recent post aboutDocker, we looked into some things that differentiate Docker containers from Virtual Machines. I also gave a brief example of creating your first Docker image. If any of that piqued your interest and you started just trying stuff, you end up like I did with a BUNCH of images now cluttering up your machine and wondering how in the world can I clean this up? Here are a few commands that will get your Docker environment under control.
List Docker Images
- docker images
Docker provides a number of simple options to allow the admin to manage the environment. Docker images for examples lets us see all the docker images on the machine.
Last login: Fri Apr 14 17:16:30 on ttys001 Danny: > docker images REPOSITORY TAG IMAGE ID CREATED SIZE oraclelinux latest 62e4327762a5 2 days ago 225 MB pythonslim latest 2f61057fee22 3 weeks ago 318 MB <none> <none> c133f715830e 3 weeks ago 310 MB sqlslim latest 6cab4fd15c1d 3 weeks ago 348 MB oels latest 9546896416f7 3 weeks ago 114 MB sqlcl latest 980f85fc564e 4 weeks ago 463 MB oraclelinux 7-slim f005b5220b05 7 weeks ago 114 MB
Wouldn’t you love to clear some of that space up?
Remove Docker Images
- docker rmi <image id>
Docker provides rmi option to delete images. Let go ahead and put this one to work and remove the first image
Danny: > docker rmi 62e4327762a5 Error response from daemon: conflict: unable to delete 62e4327762a5 (must be forced) - image is being used by stopped container be2d81554955
Well what happened? Look closely, this image is being used by a stopped container. Alright so now we need to go find that container. On to our next command to get rid of the Docker Container.
Remove Docker Containers
- docker rm <container id>
Again, Docker provides an option, rm that allows us to delete any docker containers from our local system. Before we can run this in general, we need to find out Container IDs. In the situation above, we actually have it, but let’s user Docker to find these ids for us with the ps option and the -a parameter so we can see all the Docker Containers even if they are not running.
- docker ps -a
Danny: > docker ps-a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9f9d34aee084 sqlslim "/bin/bash" 4 minutes ago Exited (0) 4 minutes ago xenodochial_darwin e27743499f8d sqlslim "/bin/bash" 4 minutes ago Exited (0) 4 minutes ago epic_boyd 63b123e4f50c oraclelinux "/bin/bash" 24 hours ago Exited (0) 24 hours ago amazing_wilson be2d81554955 oraclelinux "/bin/bash" 24 hours ago Exited (0) 24 hours ago elated_elion 9e701197cddc pythonslim "/bin/bash" 3 weeks ago Exited (0) 3 weeks ago keen_ramanujan 58726602aa40 c133f715830e "/bin/bash" 3 weeks ago Exited (0) 3 weeks ago zealous_spence 5a04b41413ce sqlslim "sql dbryant/eyeam..." 3 weeks ago Exited (0) 3 weeks ago sharp_lichterman b7c7569d21fc sqlslim "sql dbryant/eyeam..." 3 weeks ago Exited (1) 3 weeks ago naughty_carson f852641bf275 sqlslim "sql sys/eyeamd1@1..." 3 weeks ago Exited (1) 3 weeks ago kind_kare 6e3120482e96 sqlslim "sql sys/eyeamd1@1..." 3 weeks ago Exited (1) 3 weeks ago blissful_austin 47efed596e8b sqlslim "sql sys@192.168.5..." 3 weeks ago Exited (1) 3 weeks ago serene_hamilton 5b608a95492b sqlslim "sql dbryant/eyeam..." 3 weeks ago Exited (1) 3 weeks ago friendly_hodgkin bdabab59992b sqlslim "sql dbryant/eyeam..." 3 weeks ago Exited (1) 3 weeks ago gifted_albattani 566da1f2d8a3 99ad69730de1 "/bin/sh -c 'unzip..." 3 weeks ago Exited (127) 3 weeks ago loving_jones
Now we have everything we need to get rid of those container and images. By the way, did you notice this guy here (be2d81554955) about 4 rows down? That’s your offending Container keeping you from delete that image.
Stop & Remove All Docker Containers
I’m going to nuclear and stop and delete everything with a few “shortcuts.” Using the examples above, you can stop and delete individual container by simply entering the container id after the docker option. Here let’s blow them all away:
- docker stop $(docker ps -a -q)
Here I am nesting two commands that you have already seen. The docker stop command with the docker ps -a -q (the -q is for quiet — only show IDs). This in essence passes a list of Docker Container IDs to the docker stop command resulting in the stoppage of all Docker Containers.
Danny: > docker stop $(docker ps -a -q) 9f9d34aee084 e27743499f8d 63b123e4f50c be2d81554955 9e701197cddc 58726602aa40 5a04b41413ce b7c7569d21fc f852641bf275 6e3120482e96 47efed596e8b 5b608a95492b bdabab59992b 566da1f2d8a3
Now that the Containers have been stopped, I’m going to nest another command with the docker ps -a -q command to remove/delete the Container
Danny: > docker rm $(docker ps-a-q) 9f9d34aee084 e27743499f8d 63b123e4f50c be2d81554955 9e701197cddc 58726602aa40 5a04b41413ce b7c7569d21fc f852641bf275 6e3120482e96 47efed596e8b 5b608a95492b bdabab59992b 566da1f2d8a3 Danny: > docker ps-a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Now you can see that all the Container are gone and move on with removing the Docker Images.
First re-issue the docker images command to get a look at the images that are currently out there,
Danny: > docker images REPOSITORY TAG IMAGE ID CREATED SIZE oraclelinux latest 62e4327762a5 2 days ago 225 MB pythonslim latest 2f61057fee22 3 weeks ago 318 MB <none> <none> c133f715830e 3 weeks ago 310 MB sqlslim latest 6cab4fd15c1d 3 weeks ago 348 MB oels latest 9546896416f7 3 weeks ago 114 MB sqlcl latest 980f85fc564e 4 weeks ago 463 MB oraclelinux 7-slim f005b5220b05 7 weeks ago 114 MB
and make sure that we take care of that initial attempt that failed.
Danny: > dockerrmi 62e4327762a5 Untagged: oraclelinux:latest Untagged: oraclelinux@sha256:39470a3bde74c099eefe0656635718c31d199e27cdc026742257c0d445f7f7e9 Deleted: sha256:62e4327762a51cacfcca77b032e2e5adb73bdc41836ed1180d0fe8f7cebba932 Deleted: sha256:40c24f62a02f157ab14f45407e9dd284b05299b9d90c1fb899f716e42f2bd912
As you can see here, the image was successfully deleted. Now for the nuclear option! Getting rid of them all with, and you guessed it, another nested option.
- docker rmi $(docker images -q)
Danny: > docker rmi $(docker images-q) Untagged: pythonslim:latest Deleted: sha256:2f61057fee22ed529119aba221b61afce076a05b81dd374320044a2d01f932a7 Deleted: sha256:8061aead716665ce0746794932d9f96f489bc5ed73b58789837196e92c082e20 Deleted: sha256:ff49d613d58d807cd87c589498a6eac39d7700f0595c6a1a727278c6a9af1243 Deleted: sha256:c133f715830e6ddf0c861675107015edbffe6f42fab7c23d9a1be5b84c9770dd Deleted: sha256:68331ccc086054bfb451af10b330d12702b147c3e5628a6c3d8f3ef96a56ad60 Deleted: sha256:87e67f661cf0f66f4c48b4025445ec6ea88d8df5c008930a5fbf0bf43c312e57 Untagged: sqlslim:latest Deleted: sha256:6cab4fd15c1d5eb9d45d1e5e22df91ec914571eaaf2cc679d6fbdc16610ba246 Deleted: sha256:5d0a16807c54d5eaa8485843efe7c8ac8a01fc107c66bc6d3c845ab1b83f291c Deleted: sha256:3b73082d9f20f368d1bd62a10318d2300aecba7590aff9d5e0be561bea2bc587 Deleted: sha256:99ad69730de183ba793a685e3684c4f8c7618cf6d5a083fe3146b8fdab03aa8f Deleted: sha256:ec69f880eb5d70d215196fc0754ee3658886c3ae5b88b5cd0b45dfac10c47770 Untagged: oels:latest Deleted: sha256:9546896416f76c089644165d018bb0146dd58f2800edced807477093f898e42f Deleted: sha256:f2af978d2706d37570523170bcfe2c0befee0aafcdf98f7ed71df7419fed8b74 Deleted: sha256:d3ed559d48454ec158117b6e9f907ef87db26071e7c8f19515150db8d8f461a6 Untagged: sqlcl:latest Deleted: sha256:980f85fc564ee7ae9f224ad3000aef33714cebb6826e54f49ef7efdb87d4be76 Deleted: sha256:bbf0c41ed3632412acd3fa68799b68013069d519ea38376ba174bc753c26db83 Deleted: sha256:26086900ad8f2e62172ef8ee3e5ce2a0e3f6a6ebbcd038d99ff0f65fe6dff285 Deleted: sha256:ef746b0f221637bc6e833331045d3e0c3d5e9bca76dad0ac74dddca48b934ca4 Deleted: sha256:488bdfc5ae796dc06e374d0d95a60ab91f39900bc86bc3343e689e5ea865f119 Deleted: sha256:5a42e075a32bd55aa2131239a235dbaf95426a8333ef8f6ce16ba8a3af1ba3c2 Deleted: sha256:d423508a08c8c0039dd3b4baef9e8964e0925ef57c18647ce30b655a68f8848d Untagged: oraclelinux:7-slim Untagged: oraclelinux@sha256:f3a78afd456061bb897b9f2b54b568dec3973efccf2b086d602fabb94069fb6d Deleted: sha256:f005b5220b05d380d279657c268e4024e72f4141fa070e515f81a9eab5157652 Deleted: sha256:dae53bfc95d17daa53043f78a411d88955bcbaa93f3feeb91e6eac044ad81052 Danny: > docker images REPOSITORY TAG IMAGE ID CREATED SIZE Danny: >
here we have it Container and Images are now removed from your machine. Simply run your docker build command(s) recreate your images and containers.
Enjoy!