Skip to content

Instantly share code, notes, and snippets.

@brandonaaskov
Last active October 3, 2016 15:40
Show Gist options
  • Save brandonaaskov/425c29cd4d618f5a872b42e695c109aa to your computer and use it in GitHub Desktop.
Save brandonaaskov/425c29cd4d618f5a872b42e695c109aa to your computer and use it in GitHub Desktop.
Removes all lingering docker images with a repository of <none>
var _ = require('lodash')
var exec = require('child_process').exec
var removeUnnamedDockerImages = function (json) {
_.forEach(json, function (image) {
if (image.repo === '<none>') {
exec('docker rmi -f ' + image.imageId, function (rmErr, rmStdout, rmStderr) {
if (rmErr) {
return console.error(rmErr)
}
console.log('Removed ' + image.id)
})
}
})
}
exec('docker images', function (err, stdout, stderr) {
var lines = stdout.split('\n')
var json = _.reduce(lines, function (total, line, count) {
var chunks = _.compact(line.split(' '))
if (count === 0) return total
if (_.isEmpty(chunks)) return total
total.push({
repo: chunks[0],
tag: chunks[1],
imageId: chunks[2],
created: chunks[3],
size: chunks[4]
})
return total
}, [])
removeUnnamedDockerImages(json)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment