Skip to content

Instantly share code, notes, and snippets.

@w3aran
Forked from rcbop/docker-cleanup.groovy
Created July 26, 2018 02:33
Show Gist options
  • Save w3aran/4850e647c7ad92b939eab8ec909cb179 to your computer and use it in GitHub Desktop.
Save w3aran/4850e647c7ad92b939eab8ec909cb179 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'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment