Skip to content

Instantly share code, notes, and snippets.

@rcbop
Last active January 9, 2024 20:40
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save rcbop/4acabfb15db6b361698ebd76ce5646e2 to your computer and use it in GitHub Desktop.
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
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'
}
}
@ScottBrenner
Copy link

@guice
Copy link

guice commented Jan 17, 2020

Don't forget the --volumes flag, too: docker system prune -af --volumes (-af for good measure).

@rcbop
Copy link
Author

rcbop commented Jan 20, 2020

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