Created
June 1, 2017 13:27
-
-
Save ewjoachim/c473b17b3daff8034014d3eac3fc5c55 to your computer and use it in GitHub Desktop.
Stop docker containers older than 1 day old
This file contains hidden or 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
#!/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