قالب وردپرس درنا توس
Home / Tips and Tricks / How to Delete Unused Docker Resources – CloudSavvy IT

How to Delete Unused Docker Resources – CloudSavvy IT



With Docker, you can quickly package your applications as containers, allowing you to run them anywhere from your laptop to a public cloud. After using Docker for a while, you can collect a lot of redundant containers, images and other resources. Here’s how to clean them up.

Docker usually doesn’t delete anything unless you tell it to. That means that images you have drawn and containers that you have created are likely still hanging on your system, even if they are no longer in use.

Having too many redundant resources can quickly lead to excessive disk usage. It also results in a much longer output when displaying resources on the command line, making it more difficult to find the information you are looking for.

Prune dangling springs

A “dangling”

; resource is any Docker object that is not currently attached to a container. This includes Docker images, networks and volumes, as well as stopped containers. In the case of images, an image ‘dangles’ if it is not tagged and has no references.

Docker offers a single housekeeping command that allows you to clean all hanging resources and stopped containers. Unless a resource is used by a run container, it will be deleted.

docker system prune

When you run the command, a confirmation prompt is displayed with the resource types to be deleted. Type y and press Enter to proceed with the removal. You can turn off confirmation by opening the -f or --force flag.

Volumes are not deleted by default. If you delete volumes, you risk losing any permanent data that you may want to keep. To delete volumes that are not attached to a container, specify the --volumes flag to include them in the operation.

You can delete even more image data by pressing the -a (or --all) flag. This will cause Docker to delete everything unused images, not just dangling unreferenced images. Every image on your system will be deleted unless there is at least one active container that requires it.

Use filters when pruning

Filters can be used with the prune order. Filtering gives you more control over what gets deleted. Two filters are currently supported, until and timestamp

  • until – The until filter prevents the deletion of containers, images and networks created for a specific period of time. You can specify the time in various formats, such as a Unix timestamp in seconds (eg 1614890000) or a date-formatted timestamp string (eg 2021-03-04T20:30:00
  • label – The label filter restricts the deletion so that only sources with a certain label are deleted. Labels can be used with containers, images, networks and volumes. You specify labels as either label=key=value (key equal to value), label!=key=value (key not equal to value), label=key (has the specified label) or label!=key (does not have the specified label).

To use a filter, pass it to it --filter flag:

docker system prune --filter until=2021-03-04T20:30:00 --filter label=foo=bar

This command removes resources with the extension foo=bar label created before 8:30 pm on March 4. All sources that do not meet this criterion remain untouched.

Prune individual resource types

You don’t always have to use docker system pruneDocker also offers more detailed pruning commands. These can be used if you want to delete only one type of resource.

The --force and --filter flags supported by docker system prune also apply to the individual pruning commands listed below.

Prune containers

Daily Docker commands can quickly result in a large number of stopped containers. The problem is annoyed if you docker build images without the --rm flag, as it preserves intermediate build layers.

Run docker container prune to clean up stopped containers. Stopped containers are not displayed when you are running docker psTo see them, you must docker ps -a to list all containers on your system.

To delete an individual container, use the docker rm command passing the container ID. You can get this by running docker psIf the container is running, you must use the --force flag to remove it.

Prune images

Use docker image prune to remove all dangling images. Like it docker system prune, this affects images that are not tagged or referenced by any container.

Add it -a flag to delete all unused images instead. This will delete any image on your system that is not required by at least one container. Tagged images are also included so you can quickly clean up old pulls from Docker Hub.

You can delete a specific image with the docker rmi order:

docker rmi wordpress:latest

If an image has multiple tags, docker rmi will delete the given tag without actually deleting the image. It will be deleted once the last tag has been deleted.

Prune networks

Docker networks are not automatically cleaned up. Over time, you can get a good number of them. This makes your system cluttered with redundant virtual bridge devices and routing tables. Run docker network prune to destroy unused networks and undo changes to your host’s configuration.

You can delete individual networks by executing docker rm my-network

Prune volumes

Docker will never delete a volume. They store the permanent data created by your containers, so accidental deletion can have devastating consequences. Volumes can also be shared across multiple containers or remain inactive, ready to be attached to another container in the future.

If you want to prune unused volumes, you need to run docker volume pruneThis will delete all volumes that are not used by at least one container. Individual volumes can be deleted with docker volume rm my-volume

Prevent future accumulation

You can minimize the risk of excess resources piling up in the future by making sure you use the --rm flag to docker build and docker run commands.

When used with build, it will lead to the removal of intermediate build layers (marked as : in docker images) output. This can significantly reduce disk usage. Normally, for each instruction in your DockerfileYou can usually delete them unless you plan on using them as a starting point for a new image later.

Using --rm with docker run will delete the container when the command is closed. This is ideal when you are running a binary file in a single use container. Do not pass --rm to containers that you want to run permanently, or that you want to start and stop on demand. Examples include web and database servers that run a service instead of a user-supplied command.

Conclusion

Pruning your Docker resources can free up disk space and help you work with the Docker CLI. Eliminating redundant containers and images makes it easier to identify the resources you are looking for.

Docker won’t delete anything unless you tell it to. It’s worth the run docker system prune periodically to keep your system clean. You can create a cron job that will run it monthly using the until filter to create a retention period for recently added sources.


Source link