I got inspired on aerysinnovation project, however I decided to updated it to latest version and also introduce linux/arm64 platform (not only because of the fact that I'm running on mac M1) ;)
Original UPX repo - all credits for the UPX goes to them. :)
docker run --rm -w $PWD -v $PWD:$PWD vikmstr/upx:latest --best --lzma -o [compressed file name] [file name]
Here is an example how to include it in your multistage builds:
#This Dockerfile is made for cross-platform builds
#When running on standard docker build, the BUILDPLATFORM,TARGETOS and TARGETARCH are set to the host platform (where do you run docker from)
#Build stage - build the app
FROM --platform=$BUILDPLATFORM golang:1.18 as builder
RUN mkdir /app
ADD . /app
WORKDIR /app
ARG TARGETOS
ARG TARGETARCH
#Building here for the platform, adding ldflags to make the binary even smaller
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -ldflags="-s -w" -o service-built
#Compression stage - use this image
FROM --platform=$BUILDPLATFORM vikmstr/upx:latest as upx
COPY --from=builder /app/service-built /service-built
# Compress the binary - /service is output, service-built is input
RUN upx --best --lzma -o /service /service-built
#The production image.
#Using alpine as the base image, might want to use scratch if you want to get even lower with image size.
FROM --platform=$BUILDPLATFORM alpine:latest AS production
# We have to copy the output from our compression stage to our production stage
COPY --from=upx /service /bin/service
#Run it
CMD ["/bin/service"]
Content type
Image
Digest
Size
1.1 MB
Last updated
over 4 years ago
docker pull vikmstr/upx