Implement multi-stage pattern to reduce docker image from 800mb to 7mb

This commit is contained in:
Tony Ganga 2018-11-02 08:20:32 -07:00
parent 7ea0f65a02
commit 7d6186e379

View File

@ -1,10 +1,31 @@
FROM golang:1.9.3 FROM golang:alpine as builder
RUN mkdir -p /go/src/baton # get deps ca-certs and git
WORKDIR /go/src/baton RUN apk update && apk add git && apk add ca-certificates
ADD https://github.com/golang/dep/releases/download/v0.4.1/dep-linux-amd64 /usr/bin/dep
RUN chmod +x /usr/bin/dep
COPY . . # create baton user
RUN adduser -D -g '' batonuser
RUN go install # copy src and set working directory
COPY . $GOPATH/src/baton/
WORKDIR $GOPATH/src/baton/
ENTRYPOINT ["baton"] # run dep
RUN dep ensure --vendor-only
# disable support for c system libs, for use with scratch
ENV CGO_ENABLED 0
ENV GOOS linux
ENV GOARCH amd64
# build and test our binary
RUN go test -v
RUN go build -a -installsuffix cgo -o /go/bin/baton
FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /go/bin/baton /go/bin/baton
ENTRYPOINT ["/go/bin/baton"]