Build during Docker image creation or copy compiled binary?

agolangf · · 453 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Usually, I have a Makefile for my go application, and run build inside docker container via RUN directive. However I am currently completing &#34;Scalable Microservices with Kubernetes&#34; course by Google, and in a lesson which describes building docker images, they say &#34;as a best practice&#34; we should build application before (on developer&#39;s machine or CI tool), linking everything statically, and just copy resulting binary to alpine image.</p> <p>They do not elaborate on a reason, why this is considered better than building application during image creation, and I am lost on why this is better. What if during build I also generate stuff like documentation (using apidoc for example), which needs to be present in a resulting image for it to work correctly?</p> <p>Is this outdated workflow, or is there a reason why it is strictly better and should be preferred?</p> <hr/>**评论:**<br/><br/>Justinsaccount: <pre><p>Multi stage builds remove most of the reasons for building outside the container</p></pre>ChristophBerger: <pre><p>Main reason: Damn small images. </p> <p>If you build your go application inside the container that shall run the application, the final image contains the whole Go toolchain. This is unnecessary bloat. If you build outside the container, you save hundreds of MB of image size.</p> <p>Depending on the circumstances, you even might not need alpine (or any other minimal Linux env) as the base image, <code>scratch</code> is often enough.</p> <p>You still can build within a container, using a multi-stage build, as <a href="/u/Justinsaccount" rel="nofollow">/u/Justinsaccount</a> already pointed out. </p> <p>For any extra stuff your build creates, you can use COPY to move all you need from the build image to the final image.</p> <p><a href="https://medium.com/travis-on-docker/multi-stage-docker-builds-for-creating-tiny-go-images-e0e1867efe5a" rel="nofollow">Here</a> is a good introduction to using multi-stage builds with Go.</p></pre>vincentrabah: <pre><p>Hi, You came have both stage using docker multistage building! Let’s check my repo : <a href="https://github.com/itwars/Docker-multi-stage-build" rel="nofollow">https://github.com/itwars/Docker-multi-stage-build</a> BR</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

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