How to modify jdk version in docker

PHPz
Release: 2023-04-10 15:33:41
Original
2096 people have browsed it

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.

Confirm the current JDK version

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
Copy after login

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
Copy after login

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)
Copy after login

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.

Modify JDK version

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>
Copy after login

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
Copy after login

Then follow the steps below to change the JDK version:

  1. Update the package:
apt-get update
Copy after login
  1. Search for available JDK versions:
apt-cache search openjdk
Copy after login
  1. Install the required JDK version:
apt-get install openjdk-11-jre-headless
apt-get install openjdk-11-jdk-headless
Copy after login

After the installation is complete, you can use the command againjava -versionConfirm whether the version of Java in the current container has been changed to the required version.

Confirm the modified JDK 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"));
Copy after login

Also, the JDK installation path can be viewed using the following command:

update-alternatives --display java
Copy after login

This command will be displayed in The installation path corresponding to the Java version in the current container is displayed on the terminal.

Summary

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!