| by Arround The Web | No comments

Clean Up Docker Remove Old Images, Containers, and Volumes

Docker is an open-source well established platform. It is widely used to build programs and services. Docker works with different components to manage, run and share programs, such as containers, images, networks, and volume. Moreover, Docker images are used to create and manage containers. It consists of instructions to direct the container. Therefore, images are heavy in size and take up a lot of system space.

To remove the load from the system, users occasionally want to clean up the docker by removing old or unused Docker images, containers, and volume.

This write-up will illustrate cleaning up the Docker container by removing old images, containers, and volumes.

How to Clean Up Docker by Removing Images, Containers, and Volumes?

To clean restart the Docker by removing Docker images, volumes, and containers, look at the provided methods:

How to Clean Docker by Removing Containers?

To remove the old containers or unused containers, go through the provided instructions.

Step 1: List Containers

First, list down all the containers in the terminal through the “docker ps” command and copy the id of the old container you want to remove:

> docker ps -a

Step 2: Remove Container

Next, utilize the “docker rm <id>” command to remove the container. However, you can utilize the container’s name to remove the old container:

> docker rm cb885818071b

Step 3: Stop All Containers

Removing containers one by one is a difficult task. To remove all containers to completely clean restart the Docker, first stop all executing containers through the provided command:

> docker stop $(docker ps -a -q)

Step 4: Remove All Containers

Next, in order to remove all containers, use the mentioned command:

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

In the above command, “-a” is used to point all containers, and “-q” is utilized to remove containers through ids:

To verify again list down the container and check if the containers are removed or not:

> docker ps -a

The following output shows that containers have been removed from Docker:

How to Clean Docker by Removing Images?

The Docker images are frequently used to containerize the application. These are used to build and manage containers. Docker images are heavy as they contain source code, program dependencies, and other instructions.

To clean the Docker environment by removing the images, go through the provided procedure.

Step 1: Remove Unused Docker Image

To remove the Docker image that is not used by any container, simply utilize the “docker rmi <img-name>” command. The Docker users can also use the image id to remove the image:

> docker rmi golang:alpine

Step 2: Remove Used Docker Image

The image that is being used by any container can not be removed directly as shown below:

> docker rmi pythonimage

To remove the image that is linked or associated with any containers, utilize the “-f” option along with the “docker rmi” command:

> docker rmi -f pythonimage

Step 3: Remove All Images

In order to remove all images forcefully, look at the mentioned command. The provided command will remove all images by using image ids:

> docker rmi -f $(docker images -aq)

For verification, if the images are removed or not, list down all images through the “docker images -a” command:

How to Clean Docker by Removing Volume?

Docker volume is the file system mounted with a container to save the data generated by containers. The volume is also referred to as a backup system. To remove the volume, follow the provided steps.

Step 1: List All Volumes

First, list down the volume and check which volume you want to remove to clean the system. For this purpose, utilize the “docker volume ls” command:

> docker volume ls

Step 2: Remove Volume

Next, remove the volume by utilizing the “docker volume rm <volume-name>” command:

> docker volume rm my-volume

How to Clean Docker by Pruning the System?

System pruning includes removing all stopped or dangling Docker images, volumes, containers, and networks. To prune the system in Docker, the “docker system prune” command is used:

> docker system prune

Note: The “docker system prune” cannot delete the dangling volume automatically.

To remove the unused volume along with all other components, utilize the “–volumes” option along with the “docker system prune” command:

> docker system prune -a --volumes

That’s all! We have demonstrated how to clean up Docker by removing images, volumes, and containers.

Conclusion

To clean up Docker by removing old and dangling Docker components, such as images, containers, networks, and volume, simply utilize the “docker system prune -a –volumes” command. However, users can individually remove the components, such as containers that are removed by the “docker rm” command, and images through the “docker rmi” command. This write-up has demonstrated the techniques to clean up Docker.

Share Button

Source: linuxhint.com

Leave a Reply