0

I'm learning Docker and I'm trying to create the most simple and light image as possible. That's what I did.

  • helloworld directory:
$ ls
Dockerfile  helloworld.c
  • helloworld.c
#include <stdio.h>

int main() {
    printf("Hello world!\n");
    return 0;
}
  • Dockerfile
FROM scratch
COPY helloworld /
CMD ["/helloworld"]

I compiled the .c file:

$ gcc -o helloworld helloworld.c 
$ ./helloworld
Hello world!

Then I created the image:

$ docker build --tag helloworld .
[+] Building 0.2s (5/5) FINISHED                                                                                                                                                                                                     
 => [internal] load build definition from Dockerfile                                                                                                                                                                            0.0s
 => => transferring dockerfile: 93B                                                                                                                                                                                             0.0s
 => [internal] load .dockerignore                                                                                                                                                                                               0.0s
 => => transferring context: 2B                                                                                                                                                                                                 0.0s
 => [internal] load build context                                                                                                                                                                                               0.0s
 => => transferring context: 33B                                                                                                                                                                                                0.0s
 => CACHED [1/1] COPY helloworld /                                                                                                                                                                                              0.0s
 => exporting to image                                                                                                                                                                                                          0.0s
 => => exporting layers                                                                                                                                                                                                         0.0s
 => => writing image sha256:c5664c4c93727aae40daca2c1fc0aa13ea2e2c58ba2962fa8d981aa0ec09012e                                                                                                                                    0.0s
 => => naming to docker.io/library/helloworld                                            

Then when I run the container I get the error exec /helloworld: no such file or directory:

$ docker run helloworld
exec /helloworld: no such file or directory

What am I doing wrong? Should I compiled the helloworld in a different mode?

zer0uno
  • 7,521
  • 13
  • 57
  • 86
  • 2
    `gcc` by default builds a dynamically linked binary, but the `scratch` image doesn't contain the dynamic loader or any shared libraries. Build a static binary `gcc -static` or use an image that contains at least a minimum amount of shared-library infrastructure. – David Maze Jan 27 '23 at 19:32

1 Answers1

2

You need to compile at docker build time, e.g.:

FROM ubuntu
RUN apt-get update -y && apt-get install gcc -y
COPY helloworld.c .
RUN gcc -o helloworld helloworld.c
ENTRYPOINT ["./helloworld"]
$ docker build -t helloworld .
$ docker run helloworld
Hello world!
Paolo
  • 21,270
  • 6
  • 38
  • 69