From b55d99d80a93dc11da098ddd02ae5a52d363bd66 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Thu, 18 May 2023 23:44:36 +0700 Subject: [PATCH] feat: build go with -ldflags="-s -w" --- docs/2022-06-08-dockerfile-go.html | 26 ++++++++++++++++++++++---- posts/2022-06-08-dockerfile-go.md | 12 +++++++++--- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/docs/2022-06-08-dockerfile-go.html b/docs/2022-06-08-dockerfile-go.html index 229fc9b..f1023fd 100644 --- a/docs/2022-06-08-dockerfile-go.html +++ b/docs/2022-06-08-dockerfile-go.html @@ -68,7 +68,7 @@ COPY vendor . COPY . . -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v3 go build -o ./app -tags timetzdata -trimpath . +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v3 go build -o ./app -tags timetzdata -trimpath -ldflags="-s -w" . FROM gcr.io/distroless/base-debian11 @@ -136,7 +136,7 @@ don't want each time I build Dockerfile, I need to redownload Go modules.

-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v3 go build -o ./app -tags timetzdata -trimpath .
+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v3 go build -o ./app -tags timetzdata -trimpath -ldflags="-s -w" .

This is where I build Go program.

@@ -152,8 +152,9 @@ >. TLDR's newer computers are already x86-64-v3.

- -tags timetzdata to embed timezone database incase base image - does not have. -trimpath to support reproduce build. + -tags timetzdata to embed timezone database in case base + image does not have. -trimpath to support reproduce build. + -ldflags="-s -w" to strip debugging information.

FROM gcr.io/distroless/base-debian11
@@ -163,6 +164,23 @@
 ENTRYPOINT ["/app"]

Finally, I copy app to Distroless base image.

+

Thanks

+
Feel free to ask me via diff --git a/posts/2022-06-08-dockerfile-go.md b/posts/2022-06-08-dockerfile-go.md index 378e9ee..fa825a5 100644 --- a/posts/2022-06-08-dockerfile-go.md +++ b/posts/2022-06-08-dockerfile-go.md @@ -18,7 +18,7 @@ COPY go.sum . COPY vendor . COPY . . -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v3 go build -o ./app -tags timetzdata -trimpath . +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v3 go build -o ./app -tags timetzdata -trimpath -ldflags="-s -w" . FROM gcr.io/distroless/base-debian11 @@ -69,7 +69,7 @@ First is `go.mod` and `go.sum` because it defines Go modules. The second is `vendor`, this is optional but I use it because I don't want each time I build Dockerfile, I need to redownload Go modules. ```Dockerfile -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v3 go build -o ./app -tags timetzdata -trimpath . +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v3 go build -o ./app -tags timetzdata -trimpath -ldflags="-s -w" . ``` This is where I build Go program. @@ -79,8 +79,9 @@ This is where I build Go program. `GOAMD64=v3` is new since [Go 1.18](https://go.dev/doc/go1.18#amd64), I use v3 because I read about AMD64 version in [Arch Linux rfcs](https://gitlab.archlinux.org/archlinux/rfcs/-/blob/master/rfcs/0002-march.rst). TLDR's newer computers are already x86-64-v3. -`-tags timetzdata` to embed timezone database incase base image does not have. +`-tags timetzdata` to embed timezone database in case base image does not have. `-trimpath` to support reproduce build. +`-ldflags="-s -w"` to strip debugging information. ```Dockerfile FROM gcr.io/distroless/base-debian11 @@ -91,3 +92,8 @@ ENTRYPOINT ["/app"] ``` Finally, I copy `app` to Distroless base image. + +Thanks + +- [How to start a Go project in 2023](https://boyter.org/posts/how-to-start-go-project-2023/) +- [Shrink your Go binaries with this one weird trick](https://words.filippo.io/shrink-your-go-binaries-with-this-one-weird-trick/)