Multi-stage Dockerfile for Go

Multi stage 編譯的好處是用在 CI/D流程中。

編譯階段

FROM golang:1.12.1 AS builder

RUN mkdir /app
ADD . /app
WORKDIR /app
RUN CGO_ENABLE=0 GOOS=linux go build -o app ./...

FROM alpine:latest AS Production
COPY --from=builder /app .
CMD ["./app"]

// 執行編譯 (349MB)
docker build -t go-develop( or production) .

// 執行
docker run -it -p 8080:8080 go-develop(or production)