macos - 运行私有(private) docker 镜像 : standard_init_linux. go:207 时出错:执行用户进程导致 "exec format error"

标签 macos docker dockerfile docker-desktop

我正在看

standard_init_linux.go:207: exec user process caused "exec format error"



将我的 helloWorldC 镜像作为容器运行时出错。可能是什么问题呢?任何帮助表示赞赏。

我在带有 Docker 桌面社区版的 MacOS 上尝试了以下步骤。

编写了一个简单的 C++ 程序,编译并创建了一个可执行文件。
$ cat helloWorldC.cc
#include<iostream>
using namespace std;
int main() {
 cout << "###############################################################\n";
 cout << "\tHello from basic C++ HelloWorld Dockerized image \n ";
 cout << "###############################################################\n";
 return 0;
}
$

$ clang++ -o helloWorldC helloWorldC.cc

$ ./helloWorldC 
###############################################################
 Hello from basic C++ HelloWorld Dockerized image 
 ###############################################################
$

然后为我的“helloWorldC”可执行文件创建了一个简约的 Dockerfile -
$ cat Dockerfile 
FROM scratch
ADD helloWorldC /
CMD ["/helloWorldC"]
$

构建了一个 docker 镜像 -
$ docker build --tag helloworldc .
Sending build context to Docker daemon 550.9kB
Step 1/3 : FROM scratch
---> 
Step 2/3 : ADD helloWorldC /
---> 35c21b2c67c9
Step 3/3 : CMD ["/helloWorldC"]
---> Running in bc22fbf4bf85
Removing intermediate container bc22fbf4bf85
---> 96e44669461a
Successfully built 96e44669461a
Successfully tagged helloworldc:latest
$

Docker 镜像创建成功——
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
.
.
helloworldc latest 96e44669461a About a minute ago 15.4kB
registry 2 f32a97de94e1 44 hours ago 25.8MB
.
.
$

在运行 Docker 容器时,我看到以下错误。
$ docker run helloworldc
standard_init_linux.go:207: exec user process caused "exec format error"
$ 

以下是我的 docker 版本 -
$ docker version
Client: Docker Engine - Community
 Version:           18.09.1
 API version:       1.39
 Go version:        go1.10.6
 Git commit:        4c52b90
 Built:             Wed Jan  9 19:33:12 2019
 OS/Arch:           darwin/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.1
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.6
  Git commit:       4c52b90
  Built:            Wed Jan  9 19:41:49 2019
  OS/Arch:          linux/amd64
  Experimental:     false
$

最佳答案

我想建议一个替代解决方案,它使您能够编译代码并根据需要保持最终图像最小化。这可以使用 multi-stage builds 来完成。由 docker 提供。所以你的 Dockerfile 可以是这样的:

FROM gcc:5 as builder
COPY ./helloWorldC.cc /helloWorldC.cc
RUN g++ -o helloWorldC -static helloWorldC.cc && chmod +x helloWorldC

FROM scratch
COPY --from=builder /helloWorldC /helloWorldC
CMD ["/helloWorldC"]

然后在构建之后,您将拥有一个像这样的最小图像:
REPOSITORY      TAG    IMAGE ID     CREATED        SIZE
helloworldimage latest 89800885c997 22 seconds ago 2.17MB

最后,您可以毫无问题地运行它:
docker run --rm -it helloworldimage:latest
###############################################################
    Hello from basic C++ HelloWorld Dockerized image 
 ###############################################################

关于macos - 运行私有(private) docker 镜像 : standard_init_linux. go:207 时出错:执行用户进程导致 "exec format error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55082993/

相关文章:

http - 将 docker 容器端口绑定(bind)到路径

mysql - Dockerfile 上的错误指令 COPY

r - 构建 Docker 镜像并指定端口后,Shiny 应用程序未显示

java - 使用 install4j 以 root 身份启动服务

ios - 从音频文件中提取流派元数据

docker - 如何在minikube中将数据库连接到后端?

docker - 如何使用 REST API 设置 Docker 容器的名称

macos - Mountain Lion X11 库无法 ./configure

objective-c - ffmpeg 64 位 Xcode

image - 构建时可以是 docker 层 "bypassed"吗?