0

Here is my Dockerfile

FROM ubuntu:20.04

# Install java 8 with apt-get
RUN apt-get update -qq && apt-get install -yqq openjdk-8-jdk

# Install java 11 with sdkman
RUN apt-get install -yqq curl unzip zip
RUN curl -s "https://get.sdkman.io" | bash
RUN /bin/bash -c "source /root/.sdkman/bin/sdkman-init.sh; sdk install java 11.0.12-open"

# Print java version
RUN java -version

RUN java -version says java 8

In interactive mode (docker run --rm -it <image-id>), it says java 11

Using RUN /bin/bash -c "source /root/.sdkman/bin/sdkman-init.sh; java -version" in the Dockerfile gives java 11

Why ? What could explain the difference between those behaviors ? Installing java 11 with apt-get doesn't give this problem, the default java version becomes 11 no matter what.

Zarki
  • 1
  • I wouldn't expect this to usually be a problem: a Docker image usually contains a single application and its runtime, so you wouldn't usually have multiple JVMs or a version-manager tool in your image. Anything you need to invoke `source` or `.` to use will be a little awkward in Docker since environment variables will get reset after every `RUN` command. – David Maze Dec 04 '21 at 14:45
  • you set which java version you want to use by setting env 'JAVA_HOME` – Hackaholic Dec 04 '21 at 14:53
  • Furthermore, `RUN` is executed at image build time, not at container run time. --- As an aside: I would recommend to use a lightweight image for production deployment, e.g. [one of the eclipse temurin images (`hub.docker.com`)](https://hub.docker.com/_/eclipse-temurin) or even [google's distroless java image (`console.cloud.google.com`)](https://console.cloud.google.com/gcr/images/distroless/GLOBAL/java). – Turing85 Dec 04 '21 at 15:18
  • @DavidMaze I needed to use 2 JVMs because I built opencv from source using Java 8. Java 11 was causing too many problems. Then, I needed Java 11 to run my application. I'm not looking for a work around, just an explanation of this behaviour. – Zarki Dec 04 '21 at 15:58
  • Is the fundamental problem that [Using the RUN instruction in a Dockerfile with 'source' does not work](https://stackoverflow.com/questions/20635472/using-the-run-instruction-in-a-dockerfile-with-source-does-not-work)? – David Maze Dec 04 '21 at 18:19
  • @DavidMaze Not really, it does work as long as I use the next command in the same layer. The question is why am I using java 8 in the container runtime and java 11 in interactive mode ? What's the mechanism that changes this behavior ? – Zarki Dec 04 '21 at 18:46

0 Answers0