登录
首页 >  Golang >  Go问答

使用 Go 构建轻量级容器

来源:stackoverflow

时间:2024-03-06 21:27:29 237浏览 收藏

各位小伙伴们,大家好呀!看看今天我又给各位带来了什么文章?本文标题《使用 Go 构建轻量级容器》,很明显是关于Golang的文章哈哈哈,其中内容主要会涉及到等等,如果能帮到你,觉得很不错的话,欢迎各位多多点评和分享!

问题内容

我想使用 Buildah 从头开始​​构建一个小型容器映像来运行 Go 应用程序。 除了应用程序本身之外,还需要包含其他库等。我认为需要 glibc - 还有其他吗?

总而言之,我想我问的是“在 Linux 上编译的 Go 应用程序需要哪些外部依赖项?”


解决方案


@dave c 提供了正确回答此问题的信息。将 ldd 与返回的测试应用程序一起使用:

[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]$

因此,对于那些希望使用 buildah 构建最小容器的人来说,生成它的 bash 脚本如下所示:

#!/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

这会为简单的微服务生成一个 14mb 的容器! 无需担心漏洞等其他文件。

我有一个小缺陷,我无法解决入口点,因此我在启动时覆盖入口点,但要测试它运行:

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

然后只需在终端会话中键入以下内容:

curl http://localhost:8000

谢谢 dave c!

我知道这是一个很晚的答案,但它确实告诉了如何为 golang 程序构建最精简的镜像。它基于问题Deployment using image from scratch fails to start

诀窍是构建静态链接的可执行文件并将其放入名为 scratch 的空映像中。该映像仅包含一个文件,即确切的可执行文件。这是可能的最小图像。

docker 文件:

from golang:latest as builder
# the dockerfile expects the source code of the application
# to reside in ./src/ directory
copy src /src

workdir /src

# build statically linked file and strip debug information
# the dockerfile expects the `main` package to be at the root of the module
run cgo_enabled=0 go build -ldflags="-extldflags=-static -s -w" -o executable

# scratch is an empty image
from scratch
# if you need /bin/sh and a few utilities, uncomment
# the following line. it increases the image by 5.5 mb
# from alpine:latest

copy --from=builder /src/executable /executable
# copy other files if needed

entrypoint ["/executable"]

dockerfile 期望源代码位于 src 目录中


 |_ dockerfile
 |_ src/
     |_ go.mod
     |_ package_main.go # file with `package main` and `func main()`
     |_ other source files

命令 docker build ./ -t my-minimal-go 生成名为 my-minimal-go:latest 的映像

为了证明它是最小图像,将其保存到 tar 并研究内容:

docker image save my-minimal-go:latest > my-minimal-go.tar
tar tf my-minimal-go.tar

内容类似于

84ebda22f9b32043fdcb7bb70c559f0ee91cac60b4b92f1ce424662afec6d4b9.json
e622775ad65d50bc0b9f30e6ce58ee7670f752c63c3ca70caba4f9165efdca80/
e622775ad65d50bc0b9f30e6ce58ee7670f752c63c3ca70caba4f9165efdca80/version
e622775ad65d50bc0b9f30e6ce58ee7670f752c63c3ca70caba4f9165efdca80/json
e622775ad65d50bc0b9f30e6ce58ee7670f752c63c3ca70caba4f9165efdca80/layer.tar
manifest.json
repositories

并查看图像中的文件列表:

docker image save my-minimal-go:latest | tar x --wildcards '*layer.tar' -o | tar t

输出:

executable

只有一个文件,最小的图像。

好了,本文到此结束,带大家了解了《使用 Go 构建轻量级容器》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>