Go编译的二进制文件不会在Ubuntu主机上的高山Docker容器中运行

给定一个二进制文件,使用GO编译,使用GOOS = linux和GOARCH = amd64,部署到基于alpine的docker容器:3.3,如果docker引擎主机是Ubuntu(15.10),二进制文件将不会运行:
sh: /bin/artisan: not found

如果Docker引擎主机是在Mac OS X中的VirtualBox VM中部署的busyBox(它是高山的基础),那么这个相同的二进制文件(为相同的操作系统和arch编译)将运行得很好.

如果容器基于Ubuntu映像之一,这个二进制文件也会运行得很好.

任何想法这个二进制文件丢失?

这是我做的重现(成功运行在VirtualBox / busyBox在OS X未显示):

构建(使用标志构建,即使拱形匹配):

➜  artisan git:(master) ✗ GOOS=linux GOARCH=amd64 go build

检查它可以在主机上运行:

➜  artisan git:(master) ✗ ./artisan 
10:14:04.925 [ERROR] artisan: need a command,one of server,provision or build

复制到docker目录,构建,运行:

➜  artisan git:(master) ✗ cp artisan docker/build/bin/        
➜  artisan git:(master) ✗ cd docker 
➜  docker git:(master) ✗ cat Dockerfile 
FROM docker:1.10
copY build/ /
➜  docker git:(master) ✗ docker build -t artisan .
Sending build context to Docker daemon 10.15 MB
Step 1 : FROM docker:1.10
...
➜  docker git:(master) ✗ docker run -it artisan sh
/ # /bin/artisan 
sh: /bin/artisan: not found

现在将图像基础改为phusion / baseimage:

➜  docker git:(master) ✗ cat Dockerfile 
#FROM docker:1.10
FROM phusion/baseimage
copY build/ /
➜  docker git:(master) ✗ docker build -t artisan .
Sending build context to Docker daemon 10.15 MB
Step 1 : FROM phusion/baseimage
...
➜  docker git:(master) ✗ docker run -it artisan sh
# /bin/artisan
08:16:39.424 [ERROR] artisan: need a command,provision or build
认情况下,如果使用网络包,则构建可能会生成具有某些动态链接的二进制文件.到libc.您可以通过查看ldd output.bin的结果来动态检查和静态链接

我遇到了两个解决方案:

>禁用CGO,通过CGO_ENABLED = 0
>强制使用Go执行网络依赖关系,netgo通过go build -tags netgo -a -v,这是为某些平台实现的

https://golang.org/doc/go1.2

The net package requires cgo by default because the host operating system must in general mediate network call setup. On some systems,though,it is possible to use the network without cgo,and useful to do so,for instance to avoid dynamic linking. The new build tag netgo (off by default) allows the construction of a net package in pure Go on those systems where it is possible.

以上假定,唯一的CGO依赖是标准库的网络包.

相关文章

目录前言一、创建Hadoop用户二、更新apt和安装Vim编辑器三、...
原文连接:https://www.cnblogs.com/yasmi/p/5192694.html ...
电脑重启后,打开VirtualBox,发现一直用的虚拟机莫名的消失...
参见:https://blog.csdn.net/weixin_38883338/article/deta...
Ubuntu 18.04 LTS 已切换到 Netplan 来配置网络接口。Netpla...
介绍每个 Web 服务都可以通过特定的 URL 在 Internet 上访问...