使用 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知识!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习