Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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