Skip to content

Instantly share code, notes, and snippets.

@ORESoftware
Last active May 11, 2020 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ORESoftware/4f83f401821caead6e0371f78328d667 to your computer and use it in GitHub Desktop.
Save ORESoftware/4f83f401821caead6e0371f78328d667 to your computer and use it in GitHub Desktop.
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