[英]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.