OpenJDK Java 17 docker image

Apologies if this is not the correct section.

I’m trying to create an image from scratch but having difficulty with it. I searched around and didn’t really find a Dockerfile that I can use as an example.

I wanna use alpine as a base image and install openjdk17 on it. Here’s my Dockerfile

# syntax=docker/dockerfile:1

FROM alpine:3.16.0

RUN apk add --no-cache java-cacerts

ENV JAVA_HOME=/opt/openjdk-17

ENV PATH=/opt/openjdk-17/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

ENV JAVA_VERSION=17-ea+14

RUN set -eux; \
    arch="$(apk --print-arch)"; \
    case "$arch" in         'x86_64')   \
    downloadUrl='https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz'; \
    downloadSha256='0022753d0cceecacdd3a795dd4cea2bd7ffdf9dc06e22ffd1be98411742fbb44';          ;;      *) echo >&2 "error: unsupported architecture: '$arch'"; exit 1 ;; \
    esac;       wget -O openjdk.tgz "$downloadUrl";     echo "$downloadSha256 *openjdk.tgz" | sha256sum -c -;       mkdir -p "$JAVA_HOME";  tar --extract       --file openjdk.tgz      --directory "$JAVA_HOME"        --strip-components 1        --no-same-owner     ;   rm openjdk.tgz*;        rm -rf "$JAVA_HOME/lib/security/cacerts";   ln -sT /etc/ssl/certs/java/cacerts "$JAVA_HOME/lib/security/cacerts"; \
    java -Xshare:dump;      fileEncoding="$(echo 'System.out.println(System.getProperty("file.encoding"))' | jshell -s -)"; [ "$fileEncoding" = 'UTF-8' ]; \
    rm -rf ~/.java; \
    javac --version; \
    java --version

CMD ["jshell"]


RUN echo Build Complete ...

Any idea what I’m doing wrong The output error points at the RUN command

#10 14.64 + java -Xshare:dump
#10 14.64 /bin/sh: java: not found
------
executor failed running [/bin/sh -c set -eux;   arch="$(apk --print-arch)";     case "$arch" in         'x86_64')       downloadUrl='https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz';   downloadSha256='0022753d0cceecacdd3a795dd4cea2bd7ffdf9dc06e22ffd1be98411742fbb44';          ;;      *) echo >&2 "error: unsupported architecture: '$arch'"; exit 1 ;;     esac;     wget -O openjdk.tgz "$downloadUrl";     echo "$downloadSha256 *openjdk.tgz" | sha256sum -c -;       mkdir -p "$JAVA_HOME";  tar --extract       --file openjdk.tgz      --directory "$JAVA_HOME"        --strip-components 1        --no-same-owner     ;   rm openjdk.tgz*;        rm -rf "$JAVA_HOME/lib/security/cacerts";   ln -sT /etc/ssl/certs/java/cacerts "$JAVA_HOME/lib/security/cacerts";   java -Xshare:dump;      fileEncoding="$(echo 'System.out.println(System.getProperty("file.encoding"))' | jshell -s -)"; [ "$fileEncoding" = 'UTF-8' ];     rm -rf ~/.java;     javac --version;     java --version]: exit code: 127

Is it safe to assume that you copied the content of openjdk:17-jdk-alpine and made minor modifications to use a newer alpine base image?

You might want to simply the command block in the RUN instruction. The check for the architecture seems overly complicated. Also it won’t hurt to run those command in linux sh shell (to be complient with default alpine shell) and see which part causes the problem.

update: fixed broken link to the image layer details.

Hi Thank you for the response.
I did not copy the content from [openjdk:17-jdk-alpine3.14], reason being is that I wanted to do everything from scratch and learn this way.

I am new to this and I have used images from docker but now I want to set everything up from scratch like I would do if I was setting up java in my own machine, which is why I decided to take this approach.

Please also take a note of the second paragrah I wrote about troubeshooting your command in a linux sh shell to identify what causes the problem.

1 Like

I did as you advised and I seem to have found an issue when it comes to running java
it seems like, despite having the jdk, it doesn’t find the java file
I get this when I type java or java -version

usr/java/jdk-17/bin # ls -ltra
total 456
-rwxr-xr-x    1 10668    10668        12392 Dec  7  2021 serialver
-rwxr-xr-x    1 10668    10668        12392 Dec  7  2021 rmiregistry
-rwxr-xr-x    1 10668    10668        12392 Dec  7  2021 keytool
-rwxr-xr-x    1 10668    10668        12392 Dec  7  2021 jstatd
-rwxr-xr-x    1 10668    10668        12384 Dec  7  2021 jstat
-rwxr-xr-x    1 10668    10668        12424 Dec  7  2021 jstack
-rwxr-xr-x    1 10668    10668        12392 Dec  7  2021 jshell
-rwxr-xr-x    1 10668    10668        12424 Dec  7  2021 jrunscript
-rwxr-xr-x    1 10668    10668        12384 Dec  7  2021 jps
-rwxr-xr-x    1 10668    10668        12392 Dec  7  2021 jpackage
-rwxr-xr-x    1 10668    10668        12384 Dec  7  2021 jmod
-rwxr-xr-x    1 10668    10668        12416 Dec  7  2021 jmap
-rwxr-xr-x    1 10668    10668        12416 Dec  7  2021 jlink
-rwxr-xr-x    1 10668    10668        12416 Dec  7  2021 jinfo
-rwxr-xr-x    1 10668    10668        12392 Dec  7  2021 jimage
-rwxr-xr-x    1 10668    10668        12384 Dec  7  2021 jhsdb
-rwxr-xr-x    1 10668    10668        12384 Dec  7  2021 jfr
-rwxr-xr-x    1 10668    10668        12384 Dec  7  2021 jdeps
-rwxr-xr-x    1 10668    10668        12392 Dec  7  2021 jdeprscan
-rwxr-xr-x    1 10668    10668        12384 Dec  7  2021 jdb
-rwxr-xr-x    1 10668    10668        12456 Dec  7  2021 jconsole
-rwxr-xr-x    1 10668    10668        12384 Dec  7  2021 jcmd
-rwxr-xr-x    1 10668    10668        12384 Dec  7  2021 javap
-rwxr-xr-x    1 10668    10668        12424 Dec  7  2021 javadoc
-rwxr-xr-x    1 10668    10668        12416 Dec  7  2021 javac
-rwxr-xr-x    1 10668    10668        12368 Dec  7  2021 java
-rwxr-xr-x    1 10668    10668        12392 Dec  7  2021 jarsigner
-rwxr-xr-x    1 10668    10668        12384 Dec  7  2021 jar
drwxr-xr-x    2 root     root          4096 Jun  9 16:29 .
drwxr-xr-x    8 root     root          4096 Jun  9 16:29 ..
/usr/java/jdk-17/bin # java
/bin/sh: java: not found
/usr/java/jdk-17/bin # printenv
HOSTNAME=76d13ce10b23
SHLVL=1
HOME=/root
OLDPWD=/usr/java/jdk-17
TERM=xterm
JAVA_URL=https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/usr/java/jdk-17/bin
JAVA_HOME=/usr/java/jdk-17
/usr/java/jdk-17/bin # 

The downloaded java archive is not compatible with alpine out of the box, as alpine uses musl instead of glibc as its c library.

If you insist on using openjdk from Oracles website, you will need to install glibc (a short google search should yield plenty of hits on how to do that).

Or you can use openjdk from the alpine repos. An example Dockerfile would look like this:

# syntax=docker/dockerfile:1
FROM alpine:3.16.0
RUN apk add --no-cache java-cacerts openjdk17-jdk

For time beeing it will install this exact version:

/ # java --version
openjdk 17.0.3 2022-04-19
OpenJDK Runtime Environment (build 17.0.3+7-alpine-r2)
OpenJDK 64-Bit Server VM (build 17.0.3+7-alpine-r2, mixed mode, sharing)

Note: the problem is rather alpine specific and not docker specific.

1 Like

Thank you @meyay , I’ll try this.