Skip to content

Instantly share code, notes, and snippets.

@andygarfield
Created November 19, 2017 22:33
Show Gist options
  • Save andygarfield/2cc5b6bb98ddf5f4187e51a214eb5273 to your computer and use it in GitHub Desktop.
Save andygarfield/2cc5b6bb98ddf5f4187e51a214eb5273 to your computer and use it in GitHub Desktop.
Convert Dockerfile RUN's
package main
// Dockerfiles build faster when Docker caches the result of your RUN commands. But, the
// resulting image can only be small if the the RUN commands are combined into only one
// RUN command. This script converts Dockerfiles with multiple RUN commands in a row in
// a single RUN command separated by "&& \".
// Use: Run the command while in the current directory to convert the Dockerfile there,
// or give it an optional folder path.
// Install:
// Install Go
// Build with "go build r2a.go"
import (
"io/ioutil"
"os"
"strings"
)
func main() {
dir := "./"
if len(os.Args) > 1 {
arg := os.Args[1]
if !strings.HasSuffix(arg, "/") {
dir = os.Args[1] + "/"
} else {
dir = os.Args[1]
}
if !strings.HasPrefix(arg, "/") {
dir = "./" + dir
}
}
df, err := ioutil.ReadFile(dir + "Dockerfile")
if err != nil {
panic("No Dockerfile in this directory")
}
lineSlice := strings.Split(string(df), "\n")
newDF := ""
runSeq := false
for _, line := range lineSlice {
if strings.HasPrefix(line, "RUN") {
if runSeq {
newDF = newDF[:len(newDF)-1]
newDF += " && \\\n " + line[3:]
}
runSeq = true
newDF += line + "\n"
continue
} else {
runSeq = false
newDF += line + "\n"
continue
}
}
ioutil.WriteFile(dir+"Dockerfile", []byte(newDF), 0644)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment