Preface
The Java basic image in Docker mainly revolves around two mainstream Java development environments, OpenJDK and Oracle JDK, and provides multiple versions to meet different project needs.
The following are some commonly used Java basic image versions and how to use them:
OpenJDK image
- openjdk:8- Java 8 version, a very widely used version, suitable for projects that require Java 8 features.
- openjdk:11- Java 11 is an LTS (long-term support) version, recommended for the development of new projects.
- openjdk:17- Latest LTS version (as of my knowledge update), providing the latest features and performance improvements.
Oracle JDK Mirror
Although Oracle JDK image is not as common as OpenJDK, there are also official or community-maintained versions, such as:
- oraclelinux:7-jdk8ororacle/java:8(Note: The specific naming may change over time, please refer to the actual naming on Docker Hub)
- oraclelinux:7-jdk11
- oraclelinux:8-jdk17
How to use
-
Pull the mirror: First, you need to pull the required Java image from Docker Hub or other image repository. For example, to pull an OpenJDK image for Java 8, you can use the following command:
docker pull openjdk:8
-
Writing Dockerfile: Next, create a
Dockerfile
To define your Java application container. Here is a simple example based onopenjdk:8
Mirroring an application container:FROM openjdk:8 COPY . /usr/src/myapp WORKDIR /usr/src/myapp RUN javac CMD ["java", "Main"]
In this example, the basic image is specified first
openjdk:8
, and then copy all files in the current directory to the container's/usr/src/myapp
In the directory, set the working directory, compile the Java program, and finally define the commands to run when the container is started. -
Build a mirror: Included
Dockerfile
In the directory, run the following command to build your application image:docker build -t my-java-app .
-
Run the container: After the build is completed, you can run your Java application container using the following command:
docker run -it --name my-running-app my-java-app
Please select the appropriate Java image version according to your specific needs and adjust the instructions in the Dockerfile accordingly. Remember to check the latest mirror tags, because over time, new versions will be released and old versions may no longer be maintained.
Attachment: The difference between Oracle JDK and OpenJDK
Oracle JDK and OpenJDK are distributions of two Java Development Kits (JDKs), and there are some key differences between them. Both distributions provide the tools and libraries needed to do Java development, but there are some differences in licensing, support, and updates.
- 1. License:
- Oracle JDK: In the past, Oracle JDK was provided under a commercial license. Starting with JDK 11 (September 2018), Oracle has changed its licensing model. Oracle JDK now adopts a subscription model, and users need to pay for commercial support and updates after a specific version (such as JDK 11).
- OpenJDK: On the other hand, OpenJDK has an open source license (GNU General Public License Edition 2 with Classpath exceptions), allowing free use, distribution and modification of source code.
- 2. Support:
- Oracle JDK: Using the subscription model, Oracle provides commercial support and long-term updates to Oracle JDK. This includes fixes, security patches, and other updates, but requires a paid subscription.
- OpenJDK: OpenJDK is usually driven by the community, and while organizations and vendors provide support for the OpenJDK version, it is not provided directly by Oracle. The degree of support and update may vary by the organization or vendor involved.
- 3. Update:
- Oracle JDK: Oracle usually provides regular updates and long-term support (LTS) versions for its JDK. However, for LTS versions after JDK 11, updates and support may require a commercial subscription.
- OpenJDK: OpenJDK will also receive regular updates from the community, but specific distributions or vendors may vary.
- (Technical Compatibility Test Kit):
- Oracle JDK: Oracle JDK is tested for compatibility using the Java Technology Compatibility Test Suite (TCK) to ensure compliance with Java specifications.
- OpenJDK: OpenJDK can be compiled from source code and tested by the same TCK for compatibility. Some distributions may also pass TCK testing, providing a guarantee of compatibility with the Java platform.
In short, Oracle JDK was a JDK under a commercial license in the past, but has moved to the subscription model. On the other hand, OpenJDK is open source and available for free. Both distributions can be used for Java development, but the choice may depend on licensing requirements, support requirements, and preferences for open source software. Due to its open source features and community-driven development, many developers and organizations have turned to OpenJDK or other OpenJDK-based distributions.
Summarize
This is the article about how to use Java basic images OpenJDK and OracleJDK in Docker. For more related contents of Java basic images OpenJDK and OracleJDK, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!