Skip to content

Instantly share code, notes, and snippets.

@mtilson
Last active November 4, 2021 15:51
Show Gist options
  • Save mtilson/19321b07962a174036f6be8da33615b8 to your computer and use it in GitHub Desktop.
Save mtilson/19321b07962a174036f6be8da33615b8 to your computer and use it in GitHub Desktop.
how to filter docker containers by their age [docker] [bash]
# show containers in 'exited' state created more than 10 days ago (240 hours)
# change STATE to the requested container state, can be 'exited', 'created' or 'running'
# change HOURS to the requested time in hours
docker container ls -a --format \
'HOURS=240; STATE=exited; TIME_CREATED="{{.CreatedAt}}";
if [[ {{.State}} = $STATE ]]; then
TIME_CREATED=$(date -d "${TIME_CREATED% *}" +%s);
TIME_POINT=$(($(date '+%s')-$HOURS*3600));
if [[ $TIME_CREATED -lt $TIME_POINT ]] ; then
echo "{{.ID}}|{{.Names}}|{{.RunningFor}}" ;
fi ;
fi' | bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment