Skip to content

Instantly share code, notes, and snippets.

@riy
Forked from ewjoachim/stop-old-containers.sh
Created June 6, 2018 11:04
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 riy/d10649400fd22df1daf91ab395b100dd to your computer and use it in GitHub Desktop.
Save riy/d10649400fd22df1daf91ab395b100dd 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