Skip to content

Instantly share code, notes, and snippets.

@weshouman
Created January 24, 2020 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weshouman/f0398fcfb14a3d0e7c59a92cf3a3db52 to your computer and use it in GitHub Desktop.
Save weshouman/f0398fcfb14a3d0e7c59a92cf3a3db52 to your computer and use it in GitHub Desktop.
a script to get the containers based on their age
# -------
## Status
# -------
# export DISABLE_STATUS="true" # any status
export DISABLE_STATUS="" # the specified status
export RUNNING=true
# export RUNNING=false
# -----
## FROM
# -----
#export DISABLE_FROM=true # from the beginning
export DISABLE_FROM="" # from the specified date
export FROM="1 year ago"
# -----
## TILL
# -----
# a disabled till is similar till now
export DISABLE_TILL=true # till now
#export DISABLE_TILL="" # till the specified date
export TILL=now
# Date examples
# export FROM="1 year ago"
# export TILL="2 hours ago"
docker ps --format='{{.ID}}' -a \
| xargs -n1 -r docker inspect -f '{{.ID}} {{.State.Running}} {{.State.StartedAt}}' \
| awk '(ENVIRON["DISABLE_STATUS"] \
|| $2 == ENVIRON["RUNNING"]) \
&& (ENVIRON["DISABLE_FROM"] \
|| $3 >= "'$(date -d "${FROM}" -Ins --utc | sed 's/+0000/Z/')'") \
&& (ENVIRON["DISABLE_TILL"] \
|| $3 <= "'$(date -d "${TILL}" -Ins --utc | sed 's/+0000/Z/')'") \
{print $1; }'
# Reference: https://unix.stackexchange.com/a/270636
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment