Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why java requires base image to run on docker

Tags:

linux

docker

I'm trying to understand why can't I run java inside docker container without having a base OS image.
I tried the next basic dockerfile:

FROM scratch

ADD openjdk-11.0.2_linux-x64_bin.tar.gz /java

CMD ["/java/jdk-11.0.2/bin/java", "-version" ]

And got the following error:

standard_init_linux.go:207: exec user process caused "no such file or directory"

In my understanding that's probably has something to do with unmet dependencies, but I still don't understand why I need all the user-space file system that comes with base images just to run a binary executable. aren't they supposed to run natively? what's the difference between binary that's able to run standalone and one that doesn't?

In addition I'm interested to know what is that file standard_init_linux.go? does the linux kernel contains go code?

I've found some resources about base images (below), but they still doesn't provide direct answer for my question.

Resources

  • Docker: base image
  • Architecting Containers Part 1: Why Understanding User Space vs. Kernel Space Matters
  • What is the relationship between the docker host OS and the container base image OS?
like image 906
matanper Avatar asked Feb 26 '26 21:02

matanper


2 Answers

The binary has shared library dependencies. If those dependencies aren't in the system, it won't run.

As you said, it's hard to inspect the image directly, but you can take a look at the executable on your host system. Here's what it looks like on mine:

$ ldd java/jdk-11.0.2/bin/java
    linux-vdso.so.1 (0x00007ffc16fac000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fd839c97000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fd839a78000)
    libjli.so => /home/jkugelman/from-scratch-java/java/jdk-11.0.2/bin/../lib/jli/libjli.so (0x00007fd839867000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fd839663000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd839272000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fd839eb4000)

Mounting /lib and /lib64 gets it to run:

$ docker run --rm -it -v /lib:/lib -v /lib64:/lib64 from-scratch-java
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
like image 140
John Kugelman Avatar answered Mar 01 '26 18:03

John Kugelman


Shared libraries.

Executables that aren't statically linked need a linker, a loader, and shared libraries (like the standard C library) that they link against. That's provided by your OS image.

like image 32
Charles Duffy Avatar answered Mar 01 '26 17:03

Charles Duffy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!