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.
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.
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.
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!