Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to create an optimized dockerfile for go services
FROM golang:1.13 as base_img
ADD ./netrc /root/.netrc
ENV GO111MODULE='on'
ENV GOPROXY='direct'
ENV GOSUMDB='off'
RUN mkdir -p /tmp/go-api
WORKDIR /tmp/go-api
ARG github_token
ADD "https://raw.githubusercontent.com/cm/go-api/master/go.mod?token=$github_token" .
RUN go mod download
ENV PROJECT_DIR '/go/src/github.com/cm/go-api'
WORKDIR "$PROJECT_DIR"
RUN git init
RUN git remote add origin 'https://github.com/cm/go-api.git'
ARG commit_id
RUN git fetch --depth 1 origin "$commit_id"
RUN git checkout "$commit_id"
RUN go install -v
# FROM alpine:3.11.5 # alpine doesn't work b/c binary stuff
FROM golang:1.13
ENV PROJECT_DIR '/go/src/github.com/cm/go-api'
COPY --from=base_img /go/bin /go/bin
COPY --from=base_img "$PROJECT_DIR" "$PROJECT_DIR"
WORKDIR "$PROJECT_DIR"
EXPOSE 3001
ENTRYPOINT ["/go/bin/go-api"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment