How to Remove Docker Images and Containers

0

In our earlier articles, we saw how to install Docker engine on CentOS 7 / RHEL 7 / Ubuntu 16.04, and  Debian 9. As part of that, we run containers by pulling images from Docker Hub.

READ: How to install Docker Engine on CentOS 7 / RHEL 7

READ: How to install Docker Engine on Ubuntu 16.04

READ: How to install Docker Engine on Debian 9

This article will help you to remove unwanted Docker images and containers you pulled from Docker Hub or Docker Private Registry.

READ: How to Setup Docker Private Registry on CentOS 7 / Ubuntu 16.04

Remove Docker Images:

To remove Docker images, we can use docker rmi command along with image ID or repository name.

docker rmi <IMAGE ID>

OR

docker rmi <REPOSITORY>

List out the available images on your system using the following command.

docker images

Output:

REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu      latest              ccc7a11d65b1        3 days ago          120.1 MB
docker.io/wordpress   latest              c4260b289fc7        10 days ago         405.8 MB
docker.io/centos      latest              328edcd84f1b        10 days ago         192.5 MB
docker.io/mysql       latest              c73c7527c03a        2 weeks ago         412.3 MB
docker.io/fedora      latest              49236bc2f0da        3 weeks ago         230.6 MB

Choose the Docker image you want to remove and run the following command. In my case,

docker rmi c4260b289fc7

OR

docker rmi docker.io/wordpress

Output:

Untagged: docker.io/wordpress:latest
Untagged: docker.io/wordpress@sha256:632c5f09a3de6fa711b208126bcef405e844a13f7cb38a83b0045043f4b1266f
Deleted: sha256:c4260b289fc788fd3f66a2a1bb0e7697b62511861626a39c7d0676b8909afc20
Deleted: sha256:2110961a20501e6584d1f03f50cd505ced9bd5039827e9a7010e7b9096c69cd2
Deleted: sha256:e712cda6350b06680df20dc3f67cbb3460299467a687dfd47a4554949d08715b
Deleted: sha256:665a8cb6d5629102618ac9614511582e03d1623c8fe5fc405037f8bfb72c57b0
Deleted: sha256:7bbc310cf90d2c1b3b0240066af1bd30ff44fa5e0e4109b19c14670441e06b31
Deleted: sha256:f6df728ba54f30e0d216ba3a5810551301833a0583bfe7f0563f59614c25e54b
Deleted: sha256:70ab02f5fd3c72a2f8ba5b0bc56c43313f0b59f8281f3adfd5a44661d0dcdd8d
Deleted: sha256:217150f969f9f1350f400c2052e9837d08716235dc49e295b1cb213e113c50d2
Deleted: sha256:37b99f5af2f52ea8df303bf7283567f2d0b3ed087f0498b17538b1c89bcc618a
Deleted: sha256:7b456ed31909e1210207f16ea9c6a562ec0ecda80c6c437da47c50dadb5131de
Deleted: sha256:81f25a928d4b1fc8e8b52228acf1a7a47c95385688b608193f72ef673f067b26
Deleted: sha256:9305a767577b64e9520395c5952a8ccf6767d1eb3b9d60e15726cdad9adbe4b2
Deleted: sha256:203f95a609cf0c3a13547ae367c64054a0c97737d67d82c8e164c409713912ef
Deleted: sha256:7cbb3ea89c414fc8be3470459efd85ec972545c460ad6c2c7fcfaa2a13e50fbb
Deleted: sha256:d9dcc699f04b520bb533e83e7460ff4da2dcf12dfcc6be7cba8ac13f9e3c1c12
Deleted: sha256:ffad61618a7db0f43237fb3e90c6f76708fe9a3897c594dce1df88a4353edfcc
Deleted: sha256:b36ec8b16c5ea658e42c7a7bc305d8c0c3101efb85160c91a8e2b2bc3fdb6113
Deleted: sha256:72fdb054d2ddd673cd4606acf88d7512e4f86b552fc9ac493e7f3395ab610a9b

Remove All Docker Images:

Some times, you may want to clean whole Docker images in a single go. Run the following command to remove all Docker Images.

docker rmi $(docker images -q)

Remove Docker Containers:

To remove Docker containers, you can use docker rm command along with container id.

docker rm <CONTAINER ID>

List all of your Docker containers using docker ps -a command.

docker ps -a

Output:

CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
8c6be9547a71        fedora              "/bin/bash"         About a minute ago   Up About a minute                       docker-fedora
a4d876cc42d5        ubuntu              "/bin/bash"         2 minutes ago        Up 2 minutes                            docker-ubuntu
79dc279d7674        centos              "/bin/bash"         2 minutes ago        Up 2 minutes                            docker-centos

Note down the container ID of the one you want to remove. You need to stop the container first using docker stop command before removing the container. In my case,

docker stop 8c6be9547a71

Then, remove Docker container using docker rm command.

docker rm 8c6be9547a71

Note: You can also force the removal of Docker container while it is running using docker rm <CONTAINER ID> -f.

Remove All Docker Containers:

To remove all of your Docker containers in one go, run the following command.

docker rm -f $(docker ps -a -q)

That’s All.

You might also like