├── Dockerfile ├── README.md ├── docker-multistage-build-HD.jpg ├── example.go └── frame └── myframe.go /Dockerfile: -------------------------------------------------------------------------------- 1 | # Docker builder for Golang 2 | FROM golang as builder 3 | LABEL maintainer "Vincent RABAH " 4 | 5 | WORKDIR ${GOPATH}/src/github.com/user/app 6 | COPY . . 7 | RUN set -x && \ 8 | go get -d -v . && \ 9 | CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . 10 | 11 | # Docker run Golang app 12 | FROM scratch 13 | LABEL maintainer "Vincent RABAH " 14 | 15 | WORKDIR /root/ 16 | COPY --from=builder ${GOPATH}/src/github.com/user/app . 17 | CMD ["./app"] 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # An example of Docker multi-stage building with Golang 2 | 3 | ![Itwars multi-stage building Docker](docker-multistage-build-HD.jpg) 4 | 5 | 6 | ## Introduction 7 | 8 | This repository is part of my blog post on [Docker multistage builds](http://www.it-wars.com/posts/virtualisation/docker-multi-stage-builds/). 9 | 10 | ## Howto Docker multistage builds 11 | 12 | To illustrate this purpose, I use a small *golang app* that write ascii colored output. It use a lib + a local package call myframe.go. 13 | 14 | In the **Dockerfile**, *first stage build* the golang app binary, while *second stage copy* it into the final image for run purpose. 15 | 16 | ## Bonus 17 | 18 | As we produce a binary, I can make the second stage using the magic **FROM scratch** that allowed me to make a 2Mb **tiny Docker image**! 19 | -------------------------------------------------------------------------------- /docker-multistage-build-HD.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itwars/Docker-multi-stage-build/d8ac2391adb8f4da0ae7cfcc458f60826228d64a/docker-multistage-build-HD.jpg -------------------------------------------------------------------------------- /example.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/raphamorim/go-rainbow" 7 | . "github.com/user/app/frame" 8 | ) 9 | 10 | func main() { 11 | Frame() 12 | fmt.Println(rainbow.Bold(rainbow.Hex("#8E44AD", "raphael"))) 13 | fmt.Printf("%s %s %s %s %s %s %s %s", rainbow.Blue("blue"), rainbow.Black("black"), rainbow.Red("red"), rainbow.Green("green"), rainbow.Yellow("yellow"), rainbow.Magenta("magenta"), rainbow.Cyan("cyan"), rainbow.White("white")) 14 | 15 | fmt.Printf("\n%s", rainbow.Hex("#000080", "String from HEX")) 16 | fmt.Printf("\n%s", rainbow.FromInt32(0xCC66FFFF, "String from Int32")) 17 | fmt.Printf("\n%s %s %s %s %s %s %s %s", rainbow.BgBlue("bgBlue"), rainbow.BgBlack("BgBlack"), rainbow.BgRed("bgRed"), rainbow.BgGreen("bgGreen"), rainbow.BgYellow("bgYellow"), rainbow.BgMagenta("bgMagenta"), rainbow.BgCyan("bgCyan"), rainbow.BgWhite("bgWhite")) 18 | fmt.Printf("\n%s %s %s %s %s %s", rainbow.Bold("Bold"), rainbow.Dim("Dim"), rainbow.Underscore("Underscore"), rainbow.Blink("Blink"), rainbow.Reverse("Reverse"), rainbow.Hidden("Hidden")) 19 | fmt.Printf("\n%s %s %s", rainbow.Bold(rainbow.BgYellow(rainbow.Blue("Bold+BgYellow+Blue"))), rainbow.Dim(rainbow.Underscore("Dim+Underscore")), rainbow.BgMagenta(rainbow.Red("BgMagenta+Red"))) 20 | 21 | fmt.Printf("\n%s %s %s %s %s %s %s %s", rainbow.Blue("blue"), rainbow.Black("black"), rainbow.Red("red"), rainbow.Green("green"), rainbow.Yellow("yellow"), rainbow.Magenta("magenta"), rainbow.Cyan("cyan"), rainbow.White("white")) 22 | fmt.Printf("\n%s %s %s %s %s %s %s %s", rainbow.BlueBright("blue"), rainbow.Gray("Gray"), rainbow.RedBright("red"), rainbow.GreenBright("green"), rainbow.YellowBright("yellow"), rainbow.MagentaBright("magenta"), rainbow.CyanBright("cyan"), rainbow.WhiteBright("white")) 23 | 24 | fmt.Printf("\n%s %s %s %s %s %s %s %s", rainbow.BgBlueBright("bgBlue"), rainbow.BgBlackBright("BgBlack"), rainbow.BgRedBright("bgRed"), rainbow.BgGreenBright("bgGreen"), rainbow.BgYellowBright("bgYellow"), rainbow.BgMagentaBright("bgMagenta"), rainbow.BgCyanBright("bgCyan"), rainbow.BgWhiteBright("bgWhite")) 25 | 26 | fmt.Printf("\n%s %s", rainbow.BgCyan(rainbow.Yellow("Green")), rainbow.BgCyan(rainbow.YellowBright("Bright!"))) 27 | fmt.Printf("\n%s %s", rainbow.Green(rainbow.Underscore("UnderscoreBright")), rainbow.Underscore(rainbow.GreenBright("UnderscoreBright"))) 28 | fmt.Printf("\n%s %s", rainbow.BgWhite(rainbow.Red("Red")), rainbow.BgWhite(rainbow.RedBright("Bright!"))) 29 | fmt.Printf("\n%s %s", rainbow.BgWhiteBright(rainbow.Red("Red")), rainbow.BgWhiteBright(rainbow.RedBright("Bright!"))) 30 | fmt.Printf("\n%s %s\n", rainbow.BgBlack(rainbow.Green("Green")), rainbow.BgBlack(rainbow.GreenBright("Bright!"))) 31 | } 32 | -------------------------------------------------------------------------------- /frame/myframe.go: -------------------------------------------------------------------------------- 1 | package frame 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func Frame() { 8 | fmt.Println("+---------+") 9 | fmt.Println("| |") 10 | fmt.Println("+---------+") 11 | } 12 | --------------------------------------------------------------------------------