[英]Golang binary built inside Docker container, still Mach-O executable format?
I could really use some help here.我真的可以在这里使用一些帮助。 What I am trying to do is use the standard golang:1.5 Docker image to build a Go binary, then copy that binary out of the container and into a new minimal Docker container based on busybox.
我想要做的是使用标准的 golang:1.5 Docker 镜像来构建一个 Go 二进制文件,然后将该二进制文件从容器中复制到一个基于 busybox 的新的最小 Docker 容器中。 The binary is pulled out of the container using a Docker mounted volume.
使用 Docker 安装的卷将二进制文件从容器中拉出。 There are two problems so far that I have run into.
到目前为止,我遇到了两个问题。
The resulting binary on the host (and subsequently copied into the second container) still seems to be a Mach-O 64-bit executable when running the file
command.运行
file
命令时,主机上生成的二进制文件(随后复制到第二个容器中)似乎仍然是 Mach-O 64 位可执行file
。 Is the Docker container somehow getting GOOS and GOARCH from the host? Docker 容器是否以某种方式从主机获取 GOOS 和 GOARCH?
When manually running the container with bash
and building the Go binary, it now says it is an ELF executable but it is dynamically linked.当使用
bash
手动运行容器并构建 Go 二进制文件时,它现在说它是一个 ELF 可执行文件,但它是动态链接的。 I thought by default built statically linked binaries?我认为默认情况下构建静态链接的二进制文件? I could just be wrong in this assumption.
我在这个假设中可能是错误的。
Thanks in advance for any help you can provide.在此先感谢您提供的任何帮助。
EDIT: Here are the commands I am using.编辑:这是我正在使用的命令。 Hopefully this makes it a bit more clear
希望这让它更清楚一点
## golang:1.5 base image with WORKDIR set to $GOPATH/src/myproject the source
## for the project was added in when creating the 'mybuild_img' docker image
## GOPATH is set automatically in the golang image.
docker run -i -v `pwd`/jenkins/out:$GOPATH/src/myproject/jenkins/out mybuild_img:latest bash -c 'go build && cp myproject ./jenkins/out'
Once the container is done running I have a Mach-O 64-bit executable in ./jenkins/out/.容器运行完毕后,我在 ./jenkins/out/ 中有一个 Mach-O 64 位可执行文件。 I'm not sure if this is some kind of weird behavior with docker-machine/boot2docker or anything like that.
我不确定这是否是 docker-machine/boot2docker 或类似的某种奇怪行为。 Just seems really weird.
只是看起来真的很奇怪。 I have confirmed that if i set
GOOS=linux GOARCH=amd64
before the go build
command, then I do get an executable of the correct type.我已经确认,如果我在
go build
命令之前设置GOOS=linux GOARCH=amd64
,那么我确实得到了正确类型的可执行文件。 Just trying to figure out what is going on here.只是想弄清楚这里发生了什么。
It looks like you are still binding to some C libraries.看起来您仍然绑定到某些 C 库。 This is a common problem when moving a Go executable to a super minimal container.
这是将 Go 可执行文件移动到超小型容器时的常见问题。 You can roll these bound libraries into your executable by changing
go build
to CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
您可以通过将
go build
更改为CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
来将这些绑定库滚动到您的可执行文件中CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
You can find some more information on this problem and how it relates to minimal docker builds at Codeship's blog .您可以在Codeship 的博客中找到有关此问题的更多信息以及它与最小docker构建的关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.