├── Dockerfile ├── Makefile ├── README.md └── main.go /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.10-alpine as builder 2 | ADD main.go /go/src/goapp/ 3 | RUN cd /go/src/goapp/ && \ 4 | CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main . 5 | 6 | FROM scratch 7 | COPY --from=builder /go/src/goapp/main / 8 | # ENTRYPOINT ["/main"] 9 | CMD ["/main"] -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | build: 2 | docker build -t adolphlwq/goapp . -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Minimal Golang App 2 | >This repo aims to explore how to build minimal runtime for golang application. 3 | 4 | The final docker image size is: 5 | ```shell 6 | docker images | grep goapp 7 | adolphlwq/goapp latest b2b7723296db 25 seconds ago 2.01MB 8 | ``` 9 | 10 | ## Requisites 11 | - Docker 17.05 or higher 12 | - CMake 13 | 14 | ## Dockerfile 15 | ```dockerfile 16 | FROM golang:1.10-alpine as builder 17 | ADD main.go /go/src/goapp/ 18 | RUN cd /go/src/goapp/ && \ 19 | CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main . 20 | 21 | FROM scratch 22 | COPY --from=builder /go/src/goapp/main / 23 | CMD ["/main"] 24 | ``` 25 | 26 | ## Reference 27 | - [Build minimal docker container for go applications](https://blog.codeship.com/building-minimal-docker-containers-for-go-applications/) 28 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println("go minimal docker apps") 7 | } 8 | --------------------------------------------------------------------------------