<p>I've heard disabling cgo and enabling netgo tag will do the work.
What about network performance and stability with these options?
What is the production-fine way to do this?</p>
<hr/>**评论:**<br/><br/>benbrooks: <pre><p>I have been using goos="linux" with cgo_enabled=0 and copying the binary into a scratch docker image with the entrypoint set to run the binary </p></pre>mixedCase_: <pre><p>This is the one-liner I use:</p>
<pre><code>CC=$(which musl-gcc) go build --ldflags '-w -linkmode external -extldflags "-static"' server.go
</code></pre>
<p>Change server.go for the file (or files) you want compiled. You can also switch $(which musl-gcc) for your musl-gcc binary of choice, instead of the one found in $PATH.</p></pre>sonneiko: <pre><p>so this one builds binary against musl libc statically, and I can do this on non-Alpine environment, provided I have musl installed?</p></pre>mixedCase_: <pre><p>Correct.</p>
<p>This leaves the linking to gcc (specifically, musl-gcc), indicating it to statically link musl in and the <code>-w</code> flags strips debugging symbols, reducing binary size.</p>
<p>Doesn't require disabling cgo, which is a huge plus, although os/user and some other package were broken at least up until 1.5, with 1.6 supposedly fixing os/user. However I have not run any tests on that and I don't even remember which other package it was, but I do remember it being irrelevant for my web application (a RESTful, DB-hitting server).</p></pre>tex0: <pre><p>Compiling for alpine works best when you compile in Alpine. Then you don't need any magic. But beware: some things may break (noticeable) with this approach. Be sure to run your test suite in Alpine as well.</p></pre>dlsniper: <pre><p>Have a look at this PR: <a href="https://github.com/tobegit3hub/seagull/pull/62/files" rel="nofollow">https://github.com/tobegit3hub/seagull/pull/62/files</a> it shows / explains how to build the images against Alpine and how it can be done for your project as well. (Maybe I should convert those bash scripts into a small utility written in Go? #lazyme)</p></pre>sonneiko: <pre><p>this one builds the project inside Alpine container.
thanks for sharing, but I wanted to find a way to build binaries on non-Alpine machine (there are reasons for that).</p>
<p>also I can't find any info about glibc/musl/pure go difference in performance/stability.</p>
<p>Thanks for your help anyway!</p></pre>dlsniper: <pre><p>Imho it's always better to build your artifacts on the same system / configuration that's going to run them. </p></pre>sonneiko: <pre><p>p.s. Alpine uses musl instead of glibc for standard library. </p></pre>sonneiko: <pre><p>Thank you all guys for replies, will try different ways.</p></pre>DocMerlin: <pre><p>You don't need to compile in alpine, you can run go apps in scratch containers, but you need to copy over the timezone data and some ssl stuff.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传