Skip to content

Commit b3726af

Browse files
authored
chore: reduce binary size with upx (#212)
* chore: reduce binary size with upx * fix: do not use upx on s390x
1 parent e6348bf commit b3726af

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

linux/Dockerfile

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,26 @@ RUN --mount=type=cache,target=/go/pkg/mod \
1616
# Copy source & build
1717
COPY --link . .
1818

19+
# Build binary:
20+
# -a: force rebuild
21+
# -installsuffix cgo: remove cgo support
22+
# -ldflags="-w -s": omits the DWARF symbol table, symbol table and debug information
23+
# -trimpath: remove all file system paths from the compiled executable
24+
# -o /bin/ryuk: output binary to /bin/ryuk
1925
RUN --mount=type=cache,target=/go/pkg/mod \
2026
--mount=type=cache,target=/root/.cache/go-build \
21-
go build -ldflags '-s' -o /bin/ryuk
27+
go build \
28+
-a \
29+
-installsuffix cgo \
30+
-ldflags="-w -s" \
31+
-trimpath \
32+
-o /bin/ryuk .
33+
34+
# Compress with UPX (trade-off: smaller size vs startup time)
35+
# Note: UPX is not available for s390x architecture
36+
RUN if [ "$(uname -m)" != "s390x" ]; then \
37+
apk add --no-cache upx && upx --best --lzma /bin/ryuk; \
38+
fi
2239

2340
# -----------------
2441
# Certificates

windows/Dockerfile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ USER ContainerUser
2121

2222
# Copy source & build
2323
COPY . .
24-
RUN go build -v -ldflags "-s" -o /bin/ryuk
24+
# Build binary:
25+
# -a: force rebuild
26+
# -installsuffix cgo: remove cgo support
27+
# -ldflags="-w -s": omits the DWARF symbol table, symbol table and debug information
28+
# -trimpath: remove all file system paths from the compiled executable
29+
# -o /bin/ryuk: output binary to /bin/ryuk
30+
RUN go build -a -installsuffix cgo -ldflags="-w -s" -trimpath -o /bin/ryuk .
31+
32+
# Do not optimise with UPX on Windows, as nanoserver does not have PowerShell to install it
2533

2634
# -----------------
2735
# Distributed Image

0 commit comments

Comments
 (0)