0

I am doing a very simple docker image with a c++ written program that says hello.

I had to build the executable from a virtual machine, Ubuntu 18.04, x86-64.

I launched this executable on another machine, a Windows 10 64 bits via cmd, but it throws the following:

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.

(says it's not compatible with this windows version)

When launching it with git bash, it throws:

bash: ./hello.exe: cannot execute binary file: Exec format error

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:

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

The c++ program:

#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++ -o hello.exe -static main.cpp
Itération 122442
  • 2,644
  • 2
  • 27
  • 73

1 Answers1

1

Your Dockerfile uses "scratch" image, which is a minimal (with very basic binaries to reduce the size).

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. 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. 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
ADD hello.exe /
CMD ["/hello.exe"]

Scratch is a reserved empty linux image and can run linux binaries. Since, you complied your program on ubuntu, it can run on a linux container and not on your windows machine.

As other users, changing the extension to .exe does not make it a windows executable.

Good explanation: https://stackoverflow.com/a/41556921/2777988

Further Reference: https://codeburst.io/docker-from-scratch-2a84552470c8

Rakesh Gupta
  • 3,507
  • 3
  • 18
  • 24
  • Just a quick question then: how is a linux image able to use a host windows ? Is this possible thanks to docker engine ? Where can I find a source explaining how the engine allows containers to use host resources ? It seems kile I am not able to use the correct keywords on google to find that information. – Itération 122442 Jun 16 '21 at 06:21
  • 1
    This will help clear some doubts https://stackoverflow.com/a/41556921/2777988 – Rakesh Gupta Jun 16 '21 at 11:18