Forked from antonputra/gist:533fdd507f797cc30b082eed2c4f6fb4
Created
August 25, 2024 01:26
-
-
Save mvbattista/bad5f50e1a0de9429879626afca5837d to your computer and use it in GitHub Desktop.
Golang Dockerfile with upx
This file contains hidden or 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
| FROM golang:1.23.0-bookworm AS build | |
| ARG upx_version=4.2.4 | |
| 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.mod ./ | |
| COPY go.sum ./ | |
| RUN go mod download && go mod verify | |
| 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=build /app/server /server | |
| ENTRYPOINT ["/server"] |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I didn't know it was possible to reduce a prod ready Golang image from 45 MB to 4 MB 🤯
Gist - https://lnkd.in/egccZreT