Golang 编译与 Docker alpine

问题及排查

使用 alpine 作为基础镜像,做了一个 Golang 程序的 Docker 镜像

在执行时提示:

standard_init_linux.go:190: exec user process caused "no such file or directory"

于是进入该容器,确认下 Golang 程序时存在的

ldd 了下,发现这个程序(redis2go),不是静态编译

/ # ldd ./redis2go
        /lib64/ld-linux-x86-64.so.2 (0x7f7407d36000)
        libpthread.so.0 => /lib64/ld-linux-x86-64.so.2 (0x7f7407d36000)
        libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7f7407d36000)
Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by ./redis2go)
Error relocating ./redis2go: __libc_free: symbol not found
Error relocating ./redis2go: __libc_malloc: symbol not found
Error relocating ./redis2go: __libc_realloc: symbol not found
Error relocating ./redis2go: __libc_stack_end: symbol not found

这个与 Golang 官方称已经全部重写了 C 代码有出入

因此最大的可能是程序中使用 cgo ?

然而,只是个小工具,不存在 cgo 代码

于是仔细查看了下编译脚本:

go install -race ./tools/...

发现 -race 选项,把它去掉看看

然后就 OK 了…

结论

使用 alpine 做基础镜像,或是让 Golang 程序作为全静态编译的看待的

请不要:

  • 使用 cgo 代码
  • 尽可能不要使用额外的编译选项!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fananchong2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值