Containers, Docker

Removing Docker Containers and Images

Removing Docker Containers and Images

In a recent post about Docker, 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.

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

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

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

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,

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.

  • docker rmi $(docker images -q)

There we have it Container and Images are now removed from your machine. Simply run your docker build command(s) recreate your images and containers.



For all intents and purposes, I created this post so that I could remember how I did this stuff.

Work on the go with a virtual cloud desktop from CloudDesktopOnline. Get a server to play your online multiplayer games from Apps4Rent.

Also published on Medium.

This site uses Akismet to reduce spam. Learn how your comment data is processed.