Skip to content

Instantly share code, notes, and snippets.

@eshack94
Forked from rcbop/docker-cleanup.groovy
Created October 26, 2018 15:30
Show Gist options
  • Save eshack94/491322e0606da914baeb7cf8d4232e11 to your computer and use it in GitHub Desktop.
Save eshack94/491322e0606da914baeb7cf8d4232e11 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