Docker Image and Container Clean-Up Commands

Some commands you can use to clean up running containers and your images.

Remove any containers, its recommended to verify if they are stopped first with “docker ps -a”:

docker rm $(docker ps -aq)

Remove all the images:

docker rm $(docker images -q)

In my case I had a couple of images that needed to be force removed, you can’t remove images with the above method, if they are referenced in more than one repository at a time, i.e. in my case I had some that are locally stored, and one that was stored in DockerHub.

A quick way to remove these is just to manually remove them by their image name, for example:

$ docker rmi $(docker images -q)
Untagged: web_server_image:latest
Deleted: sha256:414fb4fa561591b00474d09ae4b8eb24dfa7152d8f0e969388f4237b012631dc
Untagged: hello-world:linux
Deleted: sha256:b44f8077f3cc983f21adf071c813599ff805af75196a456a326253c7b3357c48
Untagged: hello-world:latest
Deleted: sha256:452a468a4bf985040037cb6d5392410206e47db9bf5b7278d281f94d1c2d0931
Error response from daemon: conflict: unable to delete 199b8a98ffde (must be forced) - image is referenced in multiple repositories
Error response from daemon: conflict: unable to delete 199b8a98ffde (must be forced) - image is referenced in multiple repositories

You can remove using the following:

$ docker rmi $(docker images -q)
Untagged: web_server_image:latest
Deleted: sha256:414fb4fa561591b00474d09ae4b8eb24dfa7152d8f0e969388f4237b012631dc
Untagged: hello-world:linux
Deleted: sha256:b44f8077f3cc983f21adf071c813599ff805af75196a456a326253c7b3357c48
Untagged: hello-world:latest
Deleted: sha256:452a468a4bf985040037cb6d5392410206e47db9bf5b7278d281f94d1c2d0931
Error response from daemon: conflict: unable to delete 199b8a98ffde (must be forced) - image is referenced in multiple repositories
Error response from daemon: conflict: unable to delete 199b8a98ffde (must be forced) - image is referenced in multiple repositories
$ docker rmi simple_hello_world:latest
Untagged: simple_hello_world:latest

And finally we can see it has removed one of them:

$ docker images
                                                                                                                                                                                                               i Info →   U  In Use
IMAGE                                  ID             DISK USAGE   CONTENT SIZE   EXTRA
geekmungus/simple_hello_world:v0.0.1   199b8a98ffde        117MB         29.7MB

Leave a comment