Docker is an open-source software container that allows you to easily assemble, deploy and run applications inside a self-contained isolated environment. It gives you better control of your infrastructure and helps eliminate some of the burdens in managing several containers at once. Though it sounds great on paper, many users find Docker Image Prune difficult to use because they don’t know what this feature does or how best to use it. Take a closer look at the tool’s features so you can keep those images manageable and work with confidence!
The “docker system prune” command is a way to remove old and unused images from your Docker containers. It’s a useful tool for keeping your Docker images manageable.
While focusing on deploying your applications and testing them in Docker containers, things may quickly get chaotic. You’ll have to tidy up your desk from time to time. Why not use docker image prune and other useful tools to keep your Docker environment clean?
This lesson will show you how to maintain your Docker environment tidy and manageable, as well as some best practices for dealing with unforeseen difficulties.
Let’s get going!
To follow along with this lesson, you will need the following items:
- A Windows 10 computer — This tutorial utilizes Windows 10 and uses PowerShell to perform Docker commands, although any OS that can run Docker will suffice.
- Docker Desktop v3.6.0 is used as the container host in this tutorial.
- To clean up an existing Docker container
Docker Image Removal
A Docker environment is made up of a variety of components, including images, containers, networking, and so on. There are a lot of different components to tidy up. Let’s begin by pruning any outdated photographs you no longer need. But don’t worry: if you remove images and then build a Dockerfile that requires them, Docker will download them again automatically.
The rm command is your buddy if you need to delete unneeded photographs you’ve downloaded in the past. This command removes both pictures and containers, but we’ll start with images.
If one or more containers are presently utilizing the image you want to delete, you must first use the docker stop command to stop those containers. Check out the ATA post Use Docker Stop Containers Without Screwing Things Up! for additional details.
You can’t uninstall Docker images until the containers that are utilizing them are stopped.
To use docker rm to delete an image, you must first locate the image ID. Run the docker image ls command to do so. The docker image ls command reads Docker and returns all images you have right now, along with their IDs.
To get a list of the most recently produced images, use the docker images command.
The output of the docker image ls command is shown below in a table list with the columns REPOSITORY, TAG, IMAGE ID, CREATED, and SIZE.
Getting the Image ID from a list of Docker Images
Once you have the image ID, use the docker image rm command to remove it, as shown below. Docker will delete the image with the supplied ID after you’ve done so.
# docker image rm image id # Remove just the images with the supplied image ID
Docker Images That Are ‘Dangling’
As you construct more containers and download additional photos, you’ll likely end up with a collection of images that have no related container. Docker does not yet utilize these images, which are known as ‘dangling’ or’stray’ images.
Let’s wipe up all of these hanging photos to conserve disk space and keep things neat. Run docker image prune to do this. This command prunes or removes photos that haven’t been used to create a container.
You don’t need to locate a specific image ID if you want to delete ALL images with an associated container; just run docker image prune -a to remove them all.
Docker Container Removal
It’s time to move on to containers after you’ve cleaned up your Docker images. Fortunately, uninstalling containers is quite similar to removing pictures using the rm command.
To delete containers, first use the docker container ls command to identify the container ID, as shown below. This command displays all containers, as well as the CONTAINER ID. The -al argument may be used to display all containers (running or not) and just the most recent version of each.
# List all Docker containers using docker container ls -al
To acquire the Container ID, list Docker Images.
Once you have the container ID, run docker rm with the container ID you want to delete.
# docker rm container id remove just the containers with the supplied container ID
You may also provide several container IDs by using a space to separate each ID, as seen below. The Alpine and Docker 101 instructional containers with container IDs f8ec07177218 and 6fac840d9382 are removed (rm) using this command.
6fac840d9382 docker rm f8ec07177218
All Stopped Docker Containers are being removed.
Perhaps you have a large number of halted containers using up space. Stopped containers, like hanging pictures, provide no function at the time.
To unblock all containers, follow these steps:
To list all (ls —all) Docker containers, use the docker command below. Take note of all the containers having an Exited (X) Y hours ago STATUS. These containers are all presently being shut down.
ls —all docker container
Docker Containers List
2. Remove all stopped containers using the docker container prune command. When you execute this command, you’ll see which container IDs it deleted.
Docker Container Removal
3. Finally, use docker container ls -a to verify that all halted containers have been deleted.
Docker Containers List After Removing Stray Containers
You may nest Docker commands using $() to both list and remove containers in one line with docker container rm $ if you don’t want to provide one or more container IDs manually (docker container ls -aq). The q option is used by the docker container command to prevent containers from being returned to the console. After Docker has gathered all of the containers, it executes docker container rm on each of them.
Docker Container Removal by Filtering Rules
You could have a lot of distinct containers, and deleting them all by ID would be tedious. You may design a filter to eliminate just containers that meet a given query if they all have some similar properties.
Use the filter argument or filtering flag on the docker container prune command to eliminate many containers that match a specified query. You may use the till property to filter containers produced before a certain date, or the label argument to match containers with and without a specified label.
For example, the command below will delete any halted containers that were established more than 10 hours ago.
prune —filter “until=10h” docker container
As illustrated below, type Y to confirm Docker container removal. When the removal is finished, you’ll be able to view which containers were eliminated and how much space was regained.
Unused Docker Containers from the Previous 10 Hours
Nesting Docker Commands to Clean Up Containers
All of the above instructions required you to first stop each container. What happens if the containers are already running? Instead of stopping each container separately, you may use Docker instructions nested inside a removal command to stop and remove all containers at once.
When uninstalling operating containers, take special measures. The removal of the Docker container may have an effect on certain applications or processes that rely on it.
The following nested docker ($(docker container) command shows all containers in memory silently (-aq), including those that are presently operating. After that, the list of containers is provided to the outer docker command, which removes (rm) the containers from the list.
As you can see in the example below, the nested command destroys all containers in one go. As a result, you will not get an output while checking the list of containers since the list is empty.
Containers that are both stopped and running are removed.
Using the —rm Flag to Remove Containers Automatically
Only when the Docker list has become congested are the preceding commands used to delete Docker containers. But why wait till the Docker list gets clogged? If you’re a “clean as you go” sort of worker, the —rm flag puts preventative cleaning at your fingertips.
When testing things on your system for a short period and not managing crucial files or installations within that container, the —rm option in a docker run command comes in useful.
The docker run command syntax below creates a Docker container with the name you provide (container name) and a Docker image from the Docker Hub library (organization/base image). The —rm option, on the other hand, immediately removes the container.
container name organization/base image docker run —rm
Docker Volumes Cleaning
Docker is more than simply images and containers. Storage volumes are also part of the Docker ecosystem. You may clear up storage volumes using the same basic strategy of locating IDs and trimming objects.
Docker Volumes on Windows: How to Create (and Manage)
To view all of the volume IDs on your system, run docker volume ls, just as you would with containers and images.
There are three volumes listed in this instructional, as seen below.
All the Docker Volumes are listed here.
To remove all the unneeded volumes, use the docker command below (volume prune).
Instead, execute this command to eliminate any unused Docker networks: prune the docker network
The command, as shown below, deletes all local volumes that were not in use at the time of deletion. As a result, when you try to list all the Docker volumes, all you get is an empty table list.
Unused Volumes Are Removed
Immediately removing any unneeded Docker components
You’ve been selecting and choosing which Docker components to remove throughout this tutorial. Perhaps you work in a testing environment and want to clear up everything at once. Instead of cleaning each component separately, use docker system prune to clean everything!
The docker system prune command cleans up the Docker engine by removing non-running containers, unused networks, unused images, and the build cache.
To force or suppress the warning messages and approve the deletion of Docker system parts, use the -f option, as shown: prune -f docker system
A popup appears below, asking if you want to keep deleting Docker system pieces or not. To continue, type Y to confirm.
Docker System Pruning Confirmed
You’ve learned how to clean up Docker images using the docker image prune command, remove containers with docker container rm, and more in this lesson.
Why not use your acquired knowledge to create your own preferred ways for cleaning up Docker images? Making a Docker asset inventory or implementing a strong tagging system that enables you to appropriately label your projects and prevent removing key images or containers?
The “docker image prune dry-run” is a command that allows users to remove images from their Docker container without removing them. The command will also show you what would happen if you ran the command in reality.
- docker dangling images
- docker remove unused images
- docker remove all images
- docker rmi
- docker remove all images and containers