Skip to content

Instantly share code, notes, and snippets.

@ewjoachim
Created June 1, 2017 13:27
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ewjoachim/c473b17b3daff8034014d3eac3fc5c55 to your computer and use it in GitHub Desktop.
Save ewjoachim/c473b17b3daff8034014d3eac3fc5c55 to your computer and use it in GitHub Desktop.
Stop docker containers older than 1 day old
#!/bin/bash -eux
yesterday=$(date -d yesterday +%s)
# dnsdock should not be stopped : we limit to the containers created after it
docker ps --filter since=dnsdock -q --format "{{.ID}} {{.CreatedAt}}" | while read line
do
# line looks like:
# 123456789abcdef 2017-01-01 00:00:00 +02:00 CEST
set $line
id=$1
# date doesn't like the CEST part so we skip it
date=$(date -d "$(echo ${@:2:3})" +%s)
if [ $date -le $yesterday ]; then
docker stop $id
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment