在 Docker 容器中生成一个新的进程,该容器的镜像是从头开始构建的。

huangapple go评论97阅读模式
英文:

Spawn a new process in docker container which image is built from scratch

问题

我正在尝试构建一个最小的 Docker 镜像(FROM scratch),其中包含两个可执行二进制文件。这两个二进制文件都是使用 Go 构建的。入口点设置为第一个二进制文件。它在镜像上获取一些数据,使用环境变量对其进行转换,启动一个新进程来执行第二个二进制文件,并将数据作为生成的进程的输入进行传输。

FROM scratch
COPY bin /opt/my-app
ENTRYPOINT ["/opt/my-app/first", "--run", "/opt/my-app/second"]

当我在我的 Mac 上构建这个镜像时,一切正常。但是当它在我们运行 Linux 的构建服务器上创建时,第一个进程无法启动第二个进程。它会出现错误"fork/exec /opt/my-app/second: no such file or directory"。然而,"second" 二进制文件确实存在。在这两种情况下都使用了 Docker 引擎 1.13.1。

如果将父镜像从 scratch 更改为 debian:jessie,它也可以正常工作。

我是否不知道 scratch 镜像的任何限制?

英文:

I'm trying to build a minimal docker image (FROM scratch) that contains 2 executable binaries. Both are binaries built with Go. The entrypoint is set to the first one. It takes some data on the image, transforms it using environment variables, starts a new process executing the second binary and pipes the data as an input for the spawned process.

FROM scratch
COPY bin /opt/my-app
ENTRYPOINT ["/opt/my-app/first", "--run", "/opt/my-app/second"]

When I build this image on my Mac, everything works fine. But when it's created it on our build server running linux, the first process cannot start the second one. It fails with an error "fork/exec /opt/my-app/second: no such file or directory". However, "second" binary does exist. In both cases docker engine 1.13.1 is used.

It also works if parent image is changed from scratch to debian:jessie.

Are there any limitations of the scratch image that I'm not aware of?

答案1

得分: 2

使用一个空白镜像时,没有libc(或任何共享库)。如果在Debian上正常工作,那么我怀疑二进制文件没有静态链接,这是正常的默认设置。尝试使用CGO_ENABLED=0 go build -a -installsuffix cgo进行构建,参考这里的链接:http://www.blang.io/posts/2015-04_golang-alpine-build-golang-binaries-for-alpine-linux/

英文:

With a scratch image there will not be a libc (or any shared libs). If it works fine on debian, then I suspect the binary is not statically linked, which is the normal default. Try CGO_ENABLED=0 go build -a -installsuffix cgo as seen here http://www.blang.io/posts/2015-04_golang-alpine-build-golang-binaries-for-alpine-linux/

huangapple
  • 本文由 发表于 2017年2月14日 16:32:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/42221207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定