Last active
November 4, 2021 15:51
-
-
Save mtilson/19321b07962a174036f6be8da33615b8 to your computer and use it in GitHub Desktop.
how to filter docker containers by their age [docker] [bash]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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