Docker is an open source project that makes it easy to create, deploy and run applications. Using Docker, you can easily package applications into containers and run them in any environment, allowing for consistency, scalability, and rapid deployment. When running Java applications in Docker containers, we may need to modify the version of the Java Development Kit (JDK) to meet specific needs. This article will introduce how to modify the JDK version in a Docker container.
Before starting, please first confirm the JDK version of the currently running container. You can view the version by starting the container and opening a terminal to access the container, for example:
docker run -it java:latest /bin/bash
This command will start a bash terminal within the container, with the default Java image java:latest as the base image. You can then use the following command to confirm the version of the JDK in the current container:
java -version
This command will display the version number of Java on the terminal. For example:
openjdk version "11.0.12" 2021-07-20 OpenJDK Runtime Environment (build 11.0.12+7-post-Ubuntu-2ubuntu2.21.04) OpenJDK 64-Bit Server VM (build 11.0.12+7-post-Ubuntu-2ubuntu2.21.04, mixed mode, sharing)
Of course, if you use other commands to start the container where you want to modify the JDK version, the command to confirm the JDK version may also be different.
If you need to modify the JDK version, you can run the following command to search and download the required JDK version:
apt-get update apt-cache search jdk apt-get install <jdk_package_name>
After searching for the JDK version, you can use the appropriate Run the above command with the package name to download and install a specific version of JDK.
For example, in a container running with the Docker official Java image java:latest, we change the JDK version to 11. First confirm the version of Java in the current container using the following command:
docker run -it java:latest /bin/bash java -version
Then follow the steps below to change the JDK version:
apt-get update
apt-cache search openjdk
apt-get install openjdk-11-jre-headless apt-get install openjdk-11-jdk-headless
After the installation is complete, you can use the command againjava -version
Confirm whether the version of Java in the current container has been changed to the required version.
In addition to running java -version
in the container to confirm the JDK version, you can also display the JDK version in the Java application or Check the JDK installation path in the container to confirm whether the modified JDK version has taken effect successfully.
For example, the JDK version can be displayed in a Java application through the following command:
System.out.println("JDK version: " + System.getProperty("java.version"));
Also, the JDK installation path can be viewed using the following command:
update-alternatives --display java
This command will be displayed in The installation path corresponding to the Java version in the current container is displayed on the terminal.
It is very simple to modify the JDK version in the Docker container. We just need to do it by installing the appropriate JDK version. Then, we can confirm whether the JDK version modification has taken effect successfully by displaying the JDK version in the Java application or viewing the JDK installation path in the container. Remember that before confirming the JDK version, you need to first enter the running Docker container.
The above is the detailed content of How to modify jdk version in docker. For more information, please follow other related articles on the PHP Chinese website!