July 2, 2024
Tutorials

How to Remove Docker Images, Containers, and Volumes

How to Remove Docker Images, Containers, and Volumes

Docker is a platform for creating containers, images, and volumes for development purposes.

In Docker, an image is a lightweight software package that includes all the essential requirements to run an application across different environments.

In other words, a Docker image is a snapshot of a file system along with application code, libraries, and dependencies, necessary for the execution of an application.

Containers are instances of Docker images that ensure applications run consistently, without concerns about differences in infrastructure.

Docker volumes ensure data storage, allowing smooth data management even when containers are stopped or removed.

This article covers the process of removing Docker images, containers, and volumes. Check out our article on installing docker on Ubuntu 22.04.

How to Remove Docker Images, Containers, and Volumes?

How to Remove Docker Containers?

Follow the following instructions to remove the Docker Container(s) using the terminal.

Step 1: List All Containers on Your System

In your terminal, run the command to inspect and examine the available containers in your Docker environment.

In Docker, the ps (process status) command is used for showing containers, with the -a flag denoting all:

sudo docker ps -a

When the command executes, all the containers including running and stopped will be listed on your screen.

Step 2: Stop a Particular Container

Before removing the Docker container, execute the stop command to ensure that ongoing tasks are completed and data within the container is saved properly.

sudo docker stop d73f07debb5b

The container with ID d73f07debb5b has been stopped successfully.

Step 3: Remove a Particular Container

Run the command to remove a particular container using the ID. You can alternatively use the container name instead of the container ID:

sudo docker rm d73f07debb5b

In the above command, we have used the container ID like d73f07debb5b.

Step 4: List All Containers After Removal

To check if the container has been removed from your Docker environment, run the command to view the containers list:

sudo docker ps -a

The container d73f07debb5b is no longer available on your docker.

Step 5: Remove All Stopped Containers

Docker enables you to remove all the stopped containers at once using the command:

sudo docker container prune

The provided command prompts you to confirm the removal of all stopped containers. Therefore, respond very carefully.

Step 6: Verify All Containers After Pruning

Utilize the command to check the current status of Docker containers:

sudo docker ps -a

If you notice that there is no container left on your Docker, it indicates that all the stopped containers have been removed with a single command.

How to Remove Docker Images?

You can remove the unnecessary Docker images from the Docker environment through the following steps.

Step 1: List All Images on Your System

Execute the docker command followed by the option, image ls to view all images in your Docker environment:

sudo docker image ls

Currently, we have two docker images, hello-docker, and ubuntu in our environment.

Step 2: Remove a Specific Docker Image

To remove an image from the Docker environment, execute the command with the following syntax;

sudo docker rmi 174c8c134b2a

Here you can notice that we have used the image ID instead of the image name. The main advantage of this practice is to avoid confusion, especially with the similar names of the docker image.

Step 3: Remove the Docker Image Forcefully

You can remove the Docker image without stopping it. Just add -f, which will remove the image forcefully even if it is in use by containers:

sudo docker rmi -f hello-docker

Step 4: Remove Unused Docker Images

If you want to remove the unused Docker images to clean your Docker environment, run the command:

sudo docker image prune

Step 5: Verify Docker Images After Pruning

Run the command to confirm the status of Docker images:

sudo docker image ls

The provided command displayed the updated list of Docker images after the pruning. In our case, the Docker images were not in use therefore all the images have been removed.

How to Remove Docker Volumes?

The following steps can be utilized to remove Docker volumes to manage your storage.

Step 1: List All Volumes on Your System

List all the available volumes on your Docker environment using the command:

sudo docker volume ls

There are two different volumes in our Docker environment.

Step 2: Remove a Particular Docker Volume

Utilize the docker command followed by volume rm and volume name, to remove the particular volume:

sudo docker volume rm my_vlum

Step 3: Remove Unused Docker Volumes

Similar to Docker containers and images, you can use the prune command to remove all unused Docker volumes:

sudo docker volume prune

This command removed all Docker volumes that were not used by Docker containers.

Step 4: Verify Docker Volumes After Pruning

Once you have removed the unused volumes from your Docker environment, you can verify the current status using the command:

sudo docker volume ls

You can see from the provided screenshot that all the unnecessary volumes have been cleaned from your Docker.

Conclusion

In the Docker environment, one can remove the containers, images, and volumes one by one or in bulk. Use docker rm, docker rmi, and docker volume rm to remove Docker containers, images, and volumes, respectively.

Docker offers powerful tools for running your application across different environments through containerization.

This post practically demonstrated the process of removing Docker Images, Containers, and Volume.

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video