Clean up Docker: Remove Old Images, Containers, and Volumes

28/12/2020

Clean up docker

Most users are complaining about the system/server slowness and somewhere consider to buy a new system for the server for getting good performance. Is this a good idea? In some cases, the slowness is due to the poor performance of the hardware and in that case, we may need to go with a new hardware. Other case is the system/server is overloaded with data or software.

In case of a local system, we can uninstall unwanted software or removed unwanted data to free up space and resources. Freeing the resources will improve the performance of the system. In case of web hosting servers, the software installed is required ones and we cannot uninstall the software. So other option reduces the amount hosting accounts on the server. While we considering the case of dockers, keeping unwanted images and dockers containers which are created for testing are wastage of resources. In this article, I will discuss cleaning the docker images, containers, and volumes etc.

Containers and Layers

The top writable layer is the major difference between docker images and containers. Same images can be shared by different containers. They have the separate top layer which is writable by the container and the changes made in the containers are written in this layer. Once the containers are removed, the top writable layer has removed the data from the container. You can use the following command to remove the container.

List Containers

docker ps  docker ps -a

–all , -a Show all containers (default shows just running)

You can use the option “-q” to print only the numeric ID and once it is listed, we can use the output to an input of the remove command to remove all containers listed.

–quiet , -q Only display numeric IDs

Remove containers

docker rm ID_or_Name  docker rm ID_or_Name1 ID_or_Name2

Remove all exited containers

List

docker ps -a -f status=exited

Remove

docker rm $(docker ps -a -f status=exited -q)

Stop and remove all containers

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

Docker Images

A Docker image is built up from a series of layers. Each layer represents an instruction in the image’s Dockerfile. Each layer except the very last one is read-only.

List docker image

docker images

The above command will show all top-level images, their repository, and tags and their size. Docker images have intermediate layers and they will help for reusability, decrease disk usage and speed up docker builds. By default, these intermediate docker images will not show. You can use the option “-a” to list the intermediate images too with the top level images.

docker images -a

–all , -a Show all images (default hides intermediate images)

Remove docker image

You can remove unwanted docker images using the following command.

docker rmi image_id

In case we need to remove multiple images, we can use filtering command outputs to the input of remove command. See the following example.

docker rmi image_id1 image_id2  docker rmi $(docker images -f "dangling=true" -q)

–quiet , -q Only show numeric IDs
–filter , -f Filter output based on conditions provided

There is also a prune command available in docker to remove dangling images ( images, which are not used by any containers )

docker image prune

Docker Volumes

Volumes are created individually and attached to the container for storing data. Removing the container will now remove the volume. And these volumes are not in use and are called dangling volume. You can list the volumes using list command after confirming, you can remove it.

List the docker volumes

docker volume ls  docker volume ls -f dangling=true # List dangling volumes

Remove docker volumes

docker volume rm volume_name volume_name  docker volume rm $(docker volume ls -f dangling=true -q) # Remove dangling volumes

You can use these commands to clean up your system when the systems are filled with the disk. Or you can automate the removal of dangling volumes and images using a cron.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Managing Docker Volumes using Docker Compose

Purpose of Docker Volumes Docker containers are meant to be a drop-in replacement for applications. They are meant to be...
29/12/2020

Top Web Based Docker Monitoring Tools

What is Docker Monitoring: The essence of monitoring philosophy is to ensure that the performance of a running program...
29/12/2020

How to Use Docker on Arch Linux

Today, let’s check out the usage of Docker on Arch Linux – an elite Linux distro, for the elites! Docker features The...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

mua Proxy riêng ở đâu, và nó đem lại lợi ích gì cho người sử dụng
22/11/2022

Hướng dẫn sử dụng Proxy Helper Fakeip khi thuê proxy
21/11/2022

PROXY NUÔI TÀI KHOẢN FACEBOOK – KINH NGHIỆM FAKE IP – THUÊ PROXY GIÁ RẺ
14/11/2022

Mua Proxy Nuôi Zalo Giá Rẻ Tại Onet.com.vn
14/11/2022

BẢNG GIÁ MUA PROXY VIỆT NAM và PROXY US Onet.com.vn
14/11/2022