Skip to content

Instantly share code, notes, and snippets.

@gregyjames
Created December 24, 2023 11:24
Show Gist options
  • Save gregyjames/f94e85d961f50177da24af722a2a0560 to your computer and use it in GitHub Desktop.
Save gregyjames/f94e85d961f50177da24af722a2a0560 to your computer and use it in GitHub Desktop.
Tiniest possible GO Docker image
FROM golang:alpine as golang
# Create appuser (only for scratch)
ENV USER=appuser
ENV UID=10001
RUN 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
RUN apk add --no-cache upx
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -gcflags=all="-l" -o /server .
RUN upx -9 -v --ultra-brute --lzma --best /server
# Disable scratch if you get any errors and switch to the distroless
#FROM gcr.io/distroless/static-debian11
FROM scratch
# (only for scratch)
COPY --from=golang /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=golang /etc/passwd /etc/passwd
COPY --from=golang /etc/group /etc/group
COPY --from=golang /server /app/main.lua ./
EXPOSE 8080
# (only for scratch)
USER appuser:appuser
CMD ["/server"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment