Skip to content

Instantly share code, notes, and snippets.

@lifeofguenter
Created May 10, 2021 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lifeofguenter/d28d8adc8ce61e63bf2ebc79367c2993 to your computer and use it in GitHub Desktop.
Save lifeofguenter/d28d8adc8ce61e63bf2ebc79367c2993 to your computer and use it in GitHub Desktop.
Multistep Docker build with Golang
# builder
FROM golang:1.16-buster AS builder
ARG DEBIAN_FRONTEND="noninteractive"
RUN set -ex && \
apt-get -qq update && apt-get -y -qq install \
gcc
WORKDIR /go/src/app
COPY go.* ./
RUN go mod download
COPY . .
RUN go get -d -v
RUN GOOS=linux GOARCH=amd64 go build -a -v
# main
FROM debian:buster-slim
ARG DEBIAN_FRONTEND="noninteractive"
EXPOSE 8080
ENTRYPOINT ["my-app"]
RUN set -ex &&\
apt-get -qq update && apt-get -y -qq install \
bash \
ca-certificates && \
useradd -md /app -s /bin/bash app
USER app
WORKDIR /app
COPY --from=builder --chown=app:app /go/src/app/my-app /usr/local/bin/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment