From 7d6186e37920740db1b8610ccccb6c7e7f585548 Mon Sep 17 00:00:00 2001 From: Tony Ganga Date: Fri, 2 Nov 2018 08:20:32 -0700 Subject: [PATCH] Implement multi-stage pattern to reduce docker image from 800mb to 7mb --- Dockerfile | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4a355fb..ca7eeee 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,31 @@ -FROM golang:1.9.3 +FROM golang:alpine as builder -RUN mkdir -p /go/src/baton -WORKDIR /go/src/baton +# get deps ca-certs and git +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"]