Last active
January 9, 2024 20:40
-
-
Save rcbop/4acabfb15db6b361698ebd76ce5646e2 to your computer and use it in GitHub Desktop.
Jenkins pipeline script for Docker cleanup (remove dangling images and exited containers) in a given build agent
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
node("${params.BUILD_AGENT}") { | |
stage('Dangling Containers') { | |
sh 'docker ps -q -f status=exited | xargs --no-run-if-empty docker rm' | |
} | |
stage('Dangling Images') { | |
sh 'docker images -q -f dangling=true | xargs --no-run-if-empty docker rmi' | |
} | |
stage('Dangling Volumes') { | |
sh 'docker volume ls -qf dangling=true | xargs -r docker volume rm' | |
} | |
} |
Don't forget the --volumes flag, too: docker system prune -af --volumes
(-af
for good measure).
Sorry guys, this is a very old gist, I just copied a few snippets that I had without reading and published them here.. at the time I wrote this, system prune feature was not available and it's the best way now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why not just use
docker system prune
? https://docs.docker.com/engine/reference/commandline/system_prune/