Last active
October 12, 2022 12:35
-
-
Save D3vl0per/e09d12584ff2cf09d06d3e62f42b4522 to your computer and use it in GitHub Desktop.
Best practice golang containerization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# syntax=docker/dockerfile:1.3 | |
# Language: golang | |
# Version: 1.0.1 | |
# Created by D3v | |
ARG PROJECT_NAME=ether-proxy \ | |
GO_VERSION=1.19.1 \ | |
ALPINE_VERSION=3.16 \ | |
GOOS=linux \ | |
GOARCH=amd64 \ | |
BRANCH=main \ | |
COMMIT=aaaaaaa \ | |
DEVELOPER=GitHubUser \ | |
TIMESTAMP=2022-10-12T14:30:27+02:00 \ | |
USER=appuser \ | |
UID=10001 \ | |
PORT=3333 | |
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine${ALPINE_VERSION} as builder | |
ARG USER \ | |
UID \ | |
GOOS \ | |
GOARCH | |
ENV CGO_ENABLED=0 \ | |
GO111MODULE=on \ | |
GOOS=${GOOS} \ | |
GOARCH=${GOARCH} \ | |
USER=${USER} \ | |
UID=${UID} | |
RUN apk update --no-cache \ | |
&& apk add --no-cache gcc libc-dev ca-certificates tzdata \ | |
&& update-ca-certificates \ | |
&& adduser --disabled-password --gecos "" --home "/nonexistent" --shell "/sbin/nologin" --no-create-home --uid ${UID} ${USER} | |
WORKDIR /app | |
COPY go.mod go.sum ./ | |
RUN go mod download \ | |
&& go mod verify | |
COPY . . | |
RUN go build -a -ldflags '-w -s -extldflags "-static"' -o /app/run . | |
FROM --platform=$BUILDPLATFORM scratch AS copy | |
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo | |
COPY --from=builder /etc/passwd /etc/passwd | |
COPY --from=builder /etc/group /etc/group | |
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ | |
COPY --from=builder /app/run / | |
FROM --platform=$BUILDPLATFORM scratch AS app | |
ARG PROJECT_NAME \ | |
DEVELOPER \ | |
TIMESTAMP \ | |
COMMIT \ | |
BRANCH \ | |
GO_VERSION \ | |
ALPINE_VERSION \ | |
USER \ | |
PORT | |
ENV PROJECT_NAME=${PROJECT_NAME} \ | |
DEVELOPER=${DEVELOPER} \ | |
TIMESTAMP=${TIMESTAMP} \ | |
COMMIT_SHA=${COMMIT} \ | |
COMMIT_BRANCH=${BRANCH} \ | |
GO_VERSION=${GO_VERSION} \ | |
ALPINE_VERSION=${ALPINE_VERSION} \ | |
GO_ENV=production | |
COPY --from=copy / / | |
USER ${USER}:${USER} | |
EXPOSE ${PORT} | |
CMD ["./run"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment