Docker Community Forums

Share and learn in the Docker community.

JDK 8 installed even though JDK 11 is mentioned in Dockerfile

I need to create a docker container for my app with JDK 11 installed. My docker file looks like below:

ARG env_=local
FROM openjdk:11-jdk
LABEL maintainer=myapp artifact=pro-web-admin
ADD build/libs/pro-web-admin-0.0.1-SNAPSHOT.jar pro-web-admin.jar
ENTRYPOINT ["java","-jar","/pro-web-admin.jar"]

And my docker-compose.yml looks like below:

version: '3.8'
    image: postgres
      - "5432:5432"
    network_mode: host
      - POSTGRES_USER=root
      - POSTGRES_DB=testdb
    container_name: pro-web-admin
    image: pro-web-admin
    build: ./
      - "9090:9090"
    network_mode: host
      - postgresqldb
      - USER=root

But I’m still seeing JDK version 8 inside the docker container:

Am I missing something here? I don’t see any discrepancy in the configuration. Can anyone please point out what’s wrong here? Really appreciate your valuable suggestions. Thanks in advance!

Quick guess, which is better than nothing.

Thinking update-alternatives.

Thank you for the response. How to configure this in docker-compose.yml ?
Does this mean I can’t run JDK 11 app using docker compose?

Hmm, the problem is not originated in the base image:

me@docker~$ docker run -ti openjdk:11-jdk java -version
Unable to find image 'openjdk:11-jdk' locally
11-jdk: Pulling from library/openjdk
e4c3d3e4f7b0: Pull complete
101c41d0463b: Pull complete
8275efcd805f: Pull complete
751620502a7a: Pull complete
a59da3a7d0e7: Pull complete
9c0f1dffe039: Pull complete
474314d81831: Pull complete
Digest: sha256:d8af704f3022f5c44b4340c58e62313c706301915ec00291eec0749fc5378c3b
Status: Downloaded newer image for openjdk:11-jdk
openjdk version "11.0.9" 2020-10-20
OpenJDK Runtime Environment 18.9 (build 11.0.9+11)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.9+11, mixed mode)

Your Dockerfile shows no indication of alteration of the java configuration either. No entrypoint scripts, thus no hidden actions.
What you observe is impossible with a fresh build image based on your Dockerfile.

Sure it is a fresh builld and not just an old image that happens to have the same name and tag?


me@docker~$ docker run -ti openjdk:11-jdk cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION="10 (buster)"

The container you checked is based on Alpine and deffinitly not an image based on a current version of the base image, which is based on Debian.

Looks like a pebkac :grin:

You’re right.
I was referring to an old image for docker compose and that was the reason for JDK 8.
Thank you for point it out.

Step 1: Create a Dockerfile
A Dockerfile lists the instructions needed to build a Docker image. So go to your project directory and create a file called Dockerfile. Once created let’s edit it adding the following lines:
FROM openjdk:8-jdk-alpine
LABEL Miguel Doctor
RUN apk add --no-cache curl tar bash procps
CMD [""]
Let’s explain the content of the file:
FROM openjdk:8-jdk-alpine: This is the starting point for your Dockerfile. Every Dockerfile typically starts with a FROM line. This FROM command receives as argument a basic existent docker image that we will use to build our layers on top of. The base image passed as argument is openjdk:8-jdk-alpine. This image contains a jdk version 8 already installed. The alpine version means that the image makes use of the alpine distribution, which is significantly smaller than any other Linux distribution.
LABEL Miguel Doctor LABEL command defines the maintainer of the image, in case you want to contact him/her for any reason.
The RUN command executes instructions at build time. It means that you can use it to configure the image by adding application, installing packages, defining security rules… In our case, we use the apk package management utility (included within alpine distributions) to install some tools we need to have available when running a container based on our image, like curl, tar, bash and procps .
CMD defines the command to be executed when you start a container. You only can execute one command in this way, so if you define more than one CMD instruction, only the last one will be executed. In our case we will leave it empty.
So, that’s how to define a very simple Dockerfile with java installed (open jdk8).
Step 2: Adding maven to our Dockerfile
Now we are going to update our Dockerfile by defining the maven installation on it. In order to do so, just open the Dockerfile and edit it as follows (the updates referring to the current step are commented in the script) :

FROM openjdk:8-jdk-alpine
LABEL Miguel Doctor
RUN apk add --no-cache curl tar bash procps
# Downloading and installing Maven
# 1- Define a constant with the version of maven you want to install
# 2- Define a constant with the working directory
# 3- Define the SHA key to validate the maven download
ARG SHA=b4880fb7a3d81edd190a029440cdf17f308621af68475a4fe976296e71ff4a4b546dd6d8a58aaafba334d309cc11e638c52808a4b0e818fc0fd544226d952544
# 4- Define the URL where maven can be downloaded from
# 5- Create the directories, download maven, validate the download, install it, remove downloaded file and set links
RUN mkdir -p /usr/share/maven /usr/share/maven/ref \
&& echo “Downlaoding maven” \
&& curl -fsSL -o /tmp/apache-maven.tar.gz {BASE_URL}/apache-maven-{MAVEN_VERSION}-bin.tar.gz \
&& echo “Checking download hash” \
&& echo “${SHA} /tmp/apache-maven.tar.gz”
&& echo “Unziping maven” \
&& tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
&& echo “Cleaning and setting links” \
&& rm -f /tmp/apache-maven.tar.gz \
&& ln -s /usr/share/maven/bin/mvn /usr/bin/mvn
# 6- Define environmental variables required by Maven, like Maven_Home directory and where the maven repo is located
ENV MAVEN_HOME /usr/share/maven
CMD [""]