What's the proper way to build Golang apps for Alpine-based containers?

xuanbao · · 758 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;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=&#34;linux&#34; 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 &#39;-w -linkmode external -extldflags &#34;-static&#34;&#39; 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&#39;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&#39;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&#39;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&#39;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&#39;s always better to build your artifacts on the same system / configuration that&#39;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&#39;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

758 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传