简体   繁体   English

为什么这个程序在容器中运行而不在主机上运行?

[英]Why does this program runs in a container but not on the host?

I am doing a very simple docker image with a c++ written program that says hello.我正在做一个非常简单的 docker 图像和一个 c++ 编写的程序,说你好。

I had to build the executable from a virtual machine, Ubuntu 18.04, x86-64.我必须从虚拟机 Ubuntu 18.04, x86-64 构建可执行文件。

I launched this executable on another machine, a Windows 10 64 bits via cmd, but it throws the following:我在另一台机器上启动了这个可执行文件,一个 Windows 10 64 位通过 cmd,但它抛出以下内容:

hello.exe n'est pas compatible avec la version de Windows actuellement exécutée. hello.exe n'est pas compatible avec la version de Windows actuellement exécutée。 Vérifiez dans les informations système de votre ordinateur, puis contactez l'éditeur de logiciel. Vérifiez dans les informations système de votre ordinateur, puis contactez l'éditeur de logiciel。

(says it's not compatible with this windows version) (说和这个 windows 版本不兼容)

When launching it with git bash, it throws:当使用 git bash 启动它时,它会抛出:

bash: ./hello.exe: cannot execute binary file: Exec format error bash:./hello.exe:无法执行二进制文件:执行格式错误

I was expecting this executable not to be runable from within a container, as per my understanding, it shares the host libraries.我希望这个可执行文件不能在容器内运行,据我了解,它共享主机库。 But surprisingly, it does work:但令人惊讶的是,它确实有效:

$ docker run hello
Hello! This message is coming from a container

I would like to know why it is working fine.我想知道为什么它工作正常。 I must have misunderstood something somewhere.我一定是在什么地方误会了什么。

The dockerfile: dockerfile:

FROM scratch
ADD hello.exe /
CMD ["/hello.exe"]

The c++ program: c++程序:

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello! This message is coming from a container \n ";
    return 0;
}

g++ command used to build the executable: g++ 命令用于构建可执行文件:

g++ -o hello.exe -static main.cpp

Your Dockerfile uses "scratch" image, which is a minimal (with very basic binaries to reduce the size).您的 Dockerfile 使用“临时”图像,这是一个最小的(使用非常基本的二进制文件来减小大小)。

According to Docker Hub, the scratch image is Docker's reserved empty image, which is useful in the context of building base images (such as debian and busybox) or super minimal images.根据 Docker Hub,scratch 镜像是 Docker 保留的空镜像,在构建基础镜像(例如 debian 和busybox)或超最小镜像的上下文中很有用。 As of Docker 1.5.0, FROM scratch is a no-op in the Dockerfile, and will not create an extra layer in our image.从 Docker 1.5.0 开始,从零开始在 Dockerfile 中是无操作的,并且不会在我们的映像中创建额外的层。 The FROM scratch command signals to the build process that we want the next command in the Dockerfile to be the first filesystem layer in our image. FROM scratch 命令向构建过程发出信号,我们希望 Dockerfile 中的下一个命令成为我们映像中的第一个文件系统层。

FROM scratch
ADD hello.exe /
CMD ["/hello.exe"]

Scratch is a reserved empty linux image and can run linux binaries. Scratch 是保留的空 linux 映像,可以运行 linux 二进制文件。 Since, you complied your program on ubuntu, it can run on a linux container and not on your windows machine.由于您在 ubuntu 上编译了程序,因此它可以在 linux 容器上运行,而不是在 windows 机器上运行。

As other users, changing the extension to.exe does not make it a windows executable.与其他用户一样,将扩展名更改为 .exe 不会使其成为 windows 可执行文件。

Good explanation: https://stackoverflow.com/a/41556921/2777988很好的解释: https://stackoverflow.com/a/41556921/2777988

Further Reference: https://codeburst.io/docker-from-scratch-2a84552470c8进一步参考: https://codeburst.io/docker-from-scratch-2a84552470c8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么几次运行后我的程序崩溃? - Why does my program crash after a few runs? 为什么我的程序使用的内存在运行时会不断增长? - why does the memory my program uses keep growing as it runs? 我的程序可以编译并运行,但不执行switch语句中的6个选项中的任何一个,而且我无法弄清楚为什么 - My program compiles and runs but does not do any of the 6 options in the switch statements and I cannot figure out why 为什么具有 CRT 版本 30729.4148 的嵌入清单的程序在运行时在 winsxs 文件夹中以 30729.6161(latest) 运行? - Why does the program who has a embeded manifest with CRT version 30729.4148 runs with 30729.6161(latest) in winsxs folder on runtime? 为什么OpenMP程序只在一个线程中运行 - Why OpenMP program runs only in one thread 为什么这个程序有效? - Why does this program work? 为什么这个程序挂起? - Why does this program hang? 为什么我随机生成的 integer 在整个程序中不断变化。 它可能与我在程序运行时编辑文件有关 - why does my randomly generated integer keep changing throughout my program. It might have to do with my editing files while program runs PE部分为何在两次运行之间改变? - Why does the PE sections change between runs? if语句返回0,是true/false还是程序运行成功? - Return 0 in if statement, does it means true/false or the program runs successfully?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM