brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: docker container prune --filter until= only removes old stopped containers

Docker
~/tips/tip-docker-container-prune-until

docker container prune --filter "until=24h"

Removes only stopped containers older than a given age or with a given label.

Plain docker container prune removes every stopped container. When I only want the old ones gone, a filter does the job.

This deletes stopped containers for good, so look at the list first with docker ps -a, or narrow it to the stopped ones with docker ps -a --filter status=exited.

docker container prune --filter "until=24h"

According to the Docker docs, until removes only containers created before the given timestamp. It accepts Unix timestamps, Go duration strings such as 10m or 24h computed relative to the daemon machine’s time, and date formatted timestamps such as 2017-01-04T13:10:00. The command asks “Are you sure you want to continue? [y/N]” first, and -f or --force skips that question.

You can combine filters with a label:

docker container prune --filter "label=temp" --filter "until=24h"

Filters with different keys must all match, so that removes only stopped containers that carry the label temp and were created more than 24 hours ago. Two filters with the same key work as OR: label=foo plus label=bar prunes containers with either label.

Look at the word “created”. The filter checks when the container was created, not when it stopped. A container you made a week ago and stopped five minutes ago counts as a week old and is removed.

Further reading: Docker docs: docker container prune

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link