×


Cleanup tasks with Docker How to run it quickly

Sometimes, you might need to perform cleanup tasks with docker to keep the Docker clean from unused containers or volumes.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to free up Docker memory.
In this context, we shall look into the steps to perform cleanup tasks with Docker.

How to do cleanup tasks with Docker?

Here are some tips to help you keep Docker clean.
Frequently, many Docker customers have problems due to low server space. This occurs because unwanted or unused containers take too much space to run it.
So, it gives every so often you need to clean up Docker.
The Docker cleanup tasks are not really very complicated. Also, it’s necessary to remove containers with no purpose to free up space.
Now, let us see how to do this.

1. Removing containers

Some reasons that we need to remove the container.
i. Unused container:- Not every container that created continues to be used.
ii. Forget about certain lingering containers created.

There are two commands that can run to stop and remove all containers. However, this removes all the containers that are in use, so we can avoid such command if not want to do a wholesale removal.
To begin, find out what containers on the system to remove.
To do this, run the command:

$ docker ps -a

The above command will list out both running and inactive containers on the system.
In addition, any container with a status of Exited is no longer running and can be safely deleted.
To delete the exited container, run the command:

$ docker rm CONTAINTER_ID

Here, CONTAINER_ID is the first few digits of the actual ID associated with the container.
Also, there are some situations to delete the running containers that no longer use.
Therefore, the below commands helps to stop the container and to remove the container.

$ docker stop CONTAINER_ID
$ docker rm CONTAINTER_ID


CONTAINER_ID – ID which associated with the container.

An alternative to removing all stopped containers is with the command:

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


2. Remove Docker volumes

A Docker data volume is a special directory that can bypass the Docker storage driver and communicate directly with the host file system.
Data volumes initialized when a container is created and serve as either persistent or shared data between containers.
However, the volume may remain after deleting a container. Hence called dangling volumes.
In order to  remove dangling volumes, run the following command:

$ docker volume rm $(docker volume ls -qf dangling=true)


Finally, run the below command to make sure the cleanup is complete:

$ docker volume ls -qf dangling=true | xargs -r docker volume rm


3. Use a minimal base image

Recent official Docker images use the latest Debian as the base. The storage space can be saved by using a minimal base image (such as an official Debian image) instead of working with a larger image (such as Ubuntu)
Also, If the Debian image is still too big, we use a smallerBusyBox image.
So, we can start working with BusyBox by pulling the image, starting the container, and gaining access to the BusyBox shell with the command:

$ docker run -it --rm busybox

Once inside the BusyBox container, we can start building.

[Need urgent assistance to do cleanup tasks with Docker? We are available to help you today.]


Conclusion

This tutorial will guide you on how to perform cleanup tasks with Docker which involves keeping the Docker clean to solve the server space issue that is especially important when Docker is running as a virtual machine.