The content of this article is about the method and process of making Docker Image with Java program. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Here are some requirements for Docker Image production, and then we will see how to do it.
The production process should be integrated into the project construction process
Use the official Image as the base Image
Set the correct time zone
The program in the Container is started as a non-root user
Specify the interface of the Web program
Be able to pass JVM parameters, Java System Properties, and program-defined parameters
Let’s talk about how to achieve the above points in detail:
It is recommended to use Spotify's dockerfile-maven-plugin because this plugin is the simplest to use and easy to master.
The essence of this plugin is that you write a Dockerfile (please refer to the official documentation for the specific writing method of Dockerfile). This plugin passes some parameters in to help you build a Docker Image.
So as long as you can write Dockerfile, you will use this plugin. It does not add any additional concepts.
The basic image of Java should be found in the openjdk repository, not in the outdated java repository.
openjdk repository provides a variety of image tags that may seem dazzling, but essentially there are just a few:
openjdk:<version>
openjdk:<version>-slim
openjdk:<version>-alpine
Regarding <version>
Generally speaking, just specify the larger version number. For example, you can write this in the Dockerfile:
FROM openjdk:8-alpine
In terms of size, alpine is the smallest, slim is slightly larger, and the default maximum. Therefore, you should use the alpine version as much as possible. If you find that the running environment of the program lacks something, then try to use the slim version or the default version. In terms of current experience:
If you need the operating system font library, you have to use the slim version or the default version. Programs that require operating system font libraries such as image verification codes and PDF export.
If you need some Linux standard dynamic/static link libraries, then if the alpine version does not work, try the slim version or the default version. Because the alpine version is an extremely streamlined Linux, it deletes a lot of things.
The time zone of almost all Docker Images is UTC. We need to set the time zone for the Docker Image we made ourselves:
ENV TZ=Asia/Shanghai RUN set -eux; \ ln -snf /usr/share/zoneinfo/$TZ /etc/localtime; \ echo $TZ > /etc/timezone
In Docker Image, we should use a non-root user to start the program, which requires the use of gosu.
gosu’s Dockerfile guide is here.
Remember to choose the appropriate installation method according to different basic images.
For networked applications, the exposed port must be specified in the Dockerfile, otherwise the port cannot be mapped.
EXPOSE 8080
We need to be able to pass some parameters when starting the Docker Image:
JVM parameters
Java System Properties
Program startup parameters
Here it is You need to refer to Dockerfile best practice and Docker ENTRYPOINT.
The sample project source code is here: https://github.com/chanjarster/dockerfile-examples/
All program-related things are stored under /home/java-app/
:
/home/java-app ├── docker-entrypoint.sh ├── lib │ └── java-app.jar ├── etc ├── logs └── tmp
docker-entrypoint.sh, startup script
lib, stores JAR Package
lib/java-app.jar, program JAR package
etc, store configuration files
logs, store log files
tmp, store temporary File
mvn clean package dockerfile:build
normal startup, and then visithttp://localhost:8080
:
docker run -p 8080:8080 chanjarster/dockerfile-java-examples-1:1.0-SNAPSHOT
Set JVM parameters, use JVM_OPTS
environment variables:
docker run -p 8080:8080 -e JVM_OPTS='-Xmx128M -Xms128M' chanjarster/dockerfile-java-examples-1:1.0-SNAPSHOT
Set System Properties, use JAVA_ARGS
environment variables:
docker run -p 8080:8080 -e JAVA_ARGS='-Dabc=xyz -Ddef=uvw' chanjarster/dockerfile-java-examples-1:1.0-SNAPSHOT
Provide program execution Parameters, just add them directly later:
docker run -p 8080:8080 chanjarster/dockerfile-java-examples-1:1.0-SNAPSHOT --debug
The above is the detailed content of The method and process of making Docker Image using Java program. For more information, please follow other related articles on the PHP Chinese website!