dongzhi8487 2019-07-29 22:01
浏览 123
已采纳

为Go程序构建一个最小的容器

I want to build a tiny container image from scratch using Buildah to run a Go app. Apart from the app itself, what other libraries etc need to be included. I am thinking that glibc is needed - is there anything else?

So in summary, I think I am asking "what are all the external dependencies that a compiled Go app needs on Linux?"

  • 写回答

2条回答 默认 最新

  • drjv5597 2019-07-31 21:43
    关注

    @Dave C gave the information to correctly answer this. Using ldd with the test app returned:

    [bryon@localhost resttest]$ ldd restest
        linux-vdso.so.1 (0x00007fff139fe000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fbad6ce2000)
        libc.so.6 => /lib64/libc.so.6 (0x00007fbad691f000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fbad6f02000)
    [bryon@localhost resttest]$ 
    

    So for those looking to build a minimal container with Buildah, the BASH script to generate it would look like this:

    #!/bin/bash
    #
    # Run this shell script after you have run the command: "buildah unshare"
    #
    git clone https://github.com/bryonbaker/resttest.git
    cd resttest
    go build restest.go
    
    container=$(buildah from scratch)
    mnt=$(buildah mount $container)
    mkdir $mnt/bin
    mkdir $mnt/lib64
    buildah config --workingdir /bin $container
    buildah copy $container restest /bin/restest
    buildah copy $container /lib64/libpthread.so.0 /lib64
    buildah copy $container /lib64/libc.so.6 /lib64
    buildah copy $container /lib64/ld-linux-x86-64.so.2 /lib64
    buildah config --port 8000 $container
    #
    # This step is not working properly.
    # Need to run with podman -p 8000:8000 --entrypoint /bin/restest restest:latest
    buildah config --entrypoint /bin/restest $container
    buildah commit --format docker $container restest:latest
    

    This generates a 14MB container for a simple microservice! There are no additional files to be worrying about for vulnerabilities etc.

    I have a small defect I can't work out on entrypoints so I am overriding the entrypoint on start, but to test it run:

    podman -p8000:8000 --entrypoint /bin/restest restest:latest
    

    Then just type the following in a Terminal session:

    curl http://localhost:8000
    

    So thanks Dave C!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:treenode反序列化后获取不到上一节点和下一节点,Fullpath和Handle报错
  • ¥15 一部手机能否同时用不同的app进入不同的直播间?
  • ¥15 没输出运行不了什么问题
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
  • ¥15 C#i编程中so-ir-192编码的字符集转码UTF8问题
  • ¥15 51嵌入式入门按键小项目
  • ¥30 海外项目,如何降低Google Map接口费用?
  • ¥15 fluentmeshing