I am trying to create a base image following the instructions given in official docker webpage(https://docs.docker.com/samples/library/scratch/)
docker --version
Docker version 18.09.6, build 481bc77
OS Details:
NAME="Ubuntu"
VERSION="18.04.2 LTS (Bionic Beaver)"
DockerFile:
FROM scratch
COPY hello /
CMD ["/hello"]
hello.c
#include <stdio.h>
int main()
{
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}
I am able to compile the C program and execute locally
I am able to build the image using the dockerfile but when I try to run the container, I get the below error:
# docker run -i hello
standard_init_linux.go:211: exec user process caused "no such file or directory"
#
I expect the container to run successfully and print "Hello World" on console. most of the answers provided are asking to change EOL which is not applicable as we are already on Linux and not trying to run script.
I guess you get this error because you built out a dynamic linked binary like next:
$ gcc -o hello hello.c
$ ldd hello
linux-vdso.so.1 (0x00007ffe3b1ec000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7fe1fc7000)
/lib64/ld-linux-x86-64.so.2 (0x00007f7fe25ba000)
The scratch
is really basic which do not have so many so
for your binary to dynamic link. So, you need to build out a static link binary, like:
$ gcc -o hello -static hello.c
$ ldd hello
not a dynamic executable
Then, it will be work like next:
$ docker build -t hello .
Sending build context to Docker daemon 848.4kB
Step 1/3 : FROM scratch
--->
Step 2/3 : COPY hello /
---> 9d594b34f774
Step 3/3 : CMD ["/hello"]
---> Using cache
---> 2f1bad3099d3
Successfully built 2f1bad3099d3
Successfully tagged hello:latest
$ docker run -i hello
Hello, World!
And, if you do not use scratch
image which have more .so
in the system, then you no need to build out static binary.