Created
October 20, 2023 04:52
-
-
Save reaperes/7ed59dd9fdd7782176c9182f57d1ffcc to your computer and use it in GitHub Desktop.
Remove unnecessary old containers
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
#!/bin/bash | |
# Must set container prefix | |
DELETE_REGEX="^temp-.*" | |
# protect containers regex | |
EXCLUDE_REGEX="(?!(jenkins|nginx))" | |
current=$(date +"%s") | |
deleteUntil=$(date -d '-3 days' +"%s") | |
docker ps -q --filter "name=$EXCLUDE_REGEX" --filter "name=$DELETE_REGEX" --format "{{.ID}}" | while read containerId | |
do | |
createdAtStr=$(docker inspect -f "{{.Created}}" "$containerId") | |
createdAt=$(date -d "$createdAtStr" +"%s") | |
if [ "$createdAt" -lt "$deleteUntil" ]; then | |
docker rm -f $containerId | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment