feat: build go with -ldflags="-s -w"
parent
4c6a8436bb
commit
b55d99d80a
|
@ -68,7 +68,7 @@
|
||||||
<span class="pl-k">COPY</span> vendor .
|
<span class="pl-k">COPY</span> vendor .
|
||||||
<span class="pl-k">COPY</span> . .
|
<span class="pl-k">COPY</span> . .
|
||||||
|
|
||||||
<span class="pl-k">RUN</span> CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v3 go build -o ./app -tags timetzdata -trimpath .
|
<span class="pl-k">RUN</span> CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v3 go build -o ./app -tags timetzdata -trimpath -ldflags=<span class="pl-s">"-s -w"</span> .
|
||||||
|
|
||||||
<span class="pl-k">FROM</span> gcr.io/distroless/base-debian11
|
<span class="pl-k">FROM</span> gcr.io/distroless/base-debian11
|
||||||
|
|
||||||
|
@ -136,7 +136,7 @@
|
||||||
don't want each time I build Dockerfile, I need to redownload Go modules.
|
don't want each time I build Dockerfile, I need to redownload Go modules.
|
||||||
</p>
|
</p>
|
||||||
<div class="highlight highlight-source-dockerfile">
|
<div class="highlight highlight-source-dockerfile">
|
||||||
<pre><span class="pl-k">RUN</span> CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v3 go build -o ./app -tags timetzdata -trimpath .</pre>
|
<pre><span class="pl-k">RUN</span> CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOAMD64=v3 go build -o ./app -tags timetzdata -trimpath -ldflags=<span class="pl-s">"-s -w"</span> .</pre>
|
||||||
</div>
|
</div>
|
||||||
<p>This is where I build Go program.</p>
|
<p>This is where I build Go program.</p>
|
||||||
<p>
|
<p>
|
||||||
|
@ -152,8 +152,9 @@
|
||||||
>. TLDR's newer computers are already x86-64-v3.
|
>. TLDR's newer computers are already x86-64-v3.
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<code>-tags timetzdata</code> to embed timezone database incase base image
|
<code>-tags timetzdata</code> to embed timezone database in case base
|
||||||
does not have. <code>-trimpath</code> to support reproduce build.
|
image does not have. <code>-trimpath</code> to support reproduce build.
|
||||||
|
<code>-ldflags="-s -w"</code> to strip debugging information.
|
||||||
</p>
|
</p>
|
||||||
<div class="highlight highlight-source-dockerfile">
|
<div class="highlight highlight-source-dockerfile">
|
||||||
<pre><span class="pl-k">FROM</span> gcr.io/distroless/base-debian11
|
<pre><span class="pl-k">FROM</span> gcr.io/distroless/base-debian11
|
||||||
|
@ -163,6 +164,23 @@
|
||||||
<span class="pl-k">ENTRYPOINT</span> [<span class="pl-s">"/app"</span>]</pre>
|
<span class="pl-k">ENTRYPOINT</span> [<span class="pl-s">"/app"</span>]</pre>
|
||||||
</div>
|
</div>
|
||||||
<p>Finally, I copy <code>app</code> to Distroless base image.</p>
|
<p>Finally, I copy <code>app</code> to Distroless base image.</p>
|
||||||
|
<p>Thanks</p>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
<a
|
||||||
|
href="https://boyter.org/posts/how-to-start-go-project-2023/"
|
||||||
|
rel="nofollow"
|
||||||
|
>How to start a Go project in 2023</a
|
||||||
|
>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a
|
||||||
|
href="https://words.filippo.io/shrink-your-go-binaries-with-this-one-weird-trick/"
|
||||||
|
rel="nofollow"
|
||||||
|
>Shrink your Go binaries with this one weird trick</a
|
||||||
|
>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
Feel free to ask me via
|
Feel free to ask me via
|
||||||
|
|
|
@ -18,7 +18,7 @@ COPY go.sum .
|
||||||
COPY vendor .
|
COPY vendor .
|
||||||
COPY . .
|
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
|
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.
|
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
|
```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.
|
This is where I build Go program.
|
||||||
|
@ -81,6 +81,7 @@ I use v3 because I read about AMD64 version in [Arch Linux rfcs](https://gitlab.
|
||||||
|
|
||||||
`-tags timetzdata` to embed timezone database in case base image does not have.
|
`-tags timetzdata` to embed timezone database in case base image does not have.
|
||||||
`-trimpath` to support reproduce build.
|
`-trimpath` to support reproduce build.
|
||||||
|
`-ldflags="-s -w"` to strip debugging information.
|
||||||
|
|
||||||
```Dockerfile
|
```Dockerfile
|
||||||
FROM gcr.io/distroless/base-debian11
|
FROM gcr.io/distroless/base-debian11
|
||||||
|
@ -91,3 +92,8 @@ ENTRYPOINT ["/app"]
|
||||||
```
|
```
|
||||||
|
|
||||||
Finally, I copy `app` to Distroless base image.
|
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/)
|
||||||
|
|
Loading…
Reference in New Issue