This can lead to performance degradation or unnecessary storage consumption on your host machine. To ensure optimum performance and free up valuable storage, you can perform periodic cleanup operations on the Docker ENGINE.
In this tutorial, we will learn how to cleanup the Docker overlay2 storage driver, which is the default storage driver for the Docker engine.
Requirements
Before we begin, ensure you have the following:
- A system with Docker installed and running.
- Administrative privileges to perform cleanup tasks.
With the above requirements met, we can proceed.
Docker Overlay2 Storage Driver
A storage driver in Docker is a component that is responsible for managing how Docker containers and images are stored on the host system. The storage drivers are able to handle the composition of multiple layers, images and containers.
Overlay2 is one such storage driver. The overlay2 driver is the default driver for managing image layers and container fs.
Each time you pull an image or create a container, Docker creates new layers on top of existing ones.
Docker Remove Stopped Containers
The first and most common method of cleaning up the overlay2 storage driver is to remove the stopped containers. By default, Docker will store all stopped containers on the host system.
To remove them, run the command as shown:
The command above will list the stopped containers and prompt you for confirmation before removing the containers. You can bypass the confirmation prompt with the -f flag, as shown:
Docker Delete Unused Images
In some cases, you might have unused images on your Docker platform. Whether it is multiple different images or different versions of an image, Docker will retain all the pulled images which can become inefficient. To resolve this, you can remove all unused images, with the command:
Similarly, the command above will prompt you for confirmation. Add the -f flag to remove prompt feature.
To remove any and all dangling images, use the command:
Docker Remove Unused Volumes
Another Docker object that can accumulate over time is the volume. To remove any unused volumes, you can use the volumes prune command:
Again, use the -f flag to remove the prompt and force the object deletion.
Docker Clear Old Data
In other cases, even after pruning, Docker may leave behind some unused data. To clean up this storage, you can manually remove the Docker root directory.
NOTE: This action is dangerous and will remove all the images and containers stored in the engine.
Run the command:
$ sudo docker rm -rf /var/lib/docker
$ sudo service docker start
As mentioned, this is a dangerous action and will reset all the Docker objects. Only use this method if you wish to reset your Docker instance to default.
Conclusion
In this tutorial, we learned how to cleanup Docker objects provided by the overlay2 storage driver.