Skip to content

Instantly share code, notes, and snippets.

@susimsek
Last active December 20, 2021 16:24
Show Gist options
  • Save susimsek/c5bfc5d32c7f1f83aa0813f4488d98e1 to your computer and use it in GitHub Desktop.
Save susimsek/c5bfc5d32c7f1f83aa0813f4488d98e1 to your computer and use it in GitHub Desktop.
Golang Dockerfile
FROM golang:1.17-buster as builder
ARG upx_version=3.96
RUN apt-get update && apt-get install -y --no-install-recommends xz-utils && \
curl -Ls https://github.com/upx/upx/releases/download/v${upx_version}/upx-${upx_version}-amd64_linux.tar.xz -o - | tar xvJf - -C /tmp && \
cp /tmp/upx-${upx_version}-amd64_linux/upx /usr/local/bin/ && \
chmod +x /usr/local/bin/upx && \
apt-get remove -y xz-utils && \
rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY go.* ./
RUN go mod download
COPY . ./
RUN CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -o server -a -ldflags="-s -w" -installsuffix cgo
RUN upx --ultra-brute -qq server && \
upx -t server
FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /app/server /app/server
WORKDIR /app
CMD ["./server"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment