Clean out untagged Docker images after build

This commit is contained in:
Martin Honermeyer 2016-01-02 20:01:59 +01:00
parent 4023d44674
commit 2bf7ebf219

24
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -228,6 +229,29 @@ func main() {
} }
} }
// Remove untagged images, if any
var outbuf bytes.Buffer
cmd = exec.Command("sh", "-c", "docker images | grep '^<none>' | awk '{print $3}'")
cmd.Stdout = &outbuf
cmd.Stderr = os.Stderr
trace(cmd)
err = cmd.Run()
if err != nil {
os.Exit(1)
}
if outbuf.Len() > 0 {
images := strings.Split(strings.TrimSpace(outbuf.String()), "\n")
cmd = exec.Command("docker", append([]string{"rmi"}, images...)...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
trace(cmd)
err := cmd.Run()
if err != nil {
os.Exit(1)
}
}
// Save to tarred image repository // Save to tarred image repository
if len(vargs.Save.File) != 0 { if len(vargs.Save.File) != 0 {
// if the destination directory does not exist, create it // if the destination directory does not exist, create it