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?
- How to Clean Docker by Removing Images?
- How to Clean Docker by Removing Volume?
- How to Clean Docker by Pruning the System?
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:
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:
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:
Step 4: Remove All Containers
Next, in order to remove all containers, use the mentioned command:
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:
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:
Step 2: Remove Used Docker Image
The image that is being used by any container can not be removed directly as shown below:
To remove the image that is linked or associated with any containers, utilize the “-f” option along with the “docker rmi” command:
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:
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:
Step 2: Remove Volume
Next, remove the volume by utilizing the “docker volume rm <volume-name>” command:
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:
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:
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.