Home > Java > Java Tutorial > body text

The method and process of making Docker Image using Java program

不言
Release: 2018-09-19 15:35:33
Original
2030 people have browsed it

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.

Requirements

Here are some requirements for Docker Image production, and then we will see how to do it.

  1. The production process should be integrated into the project construction process

  2. Use the official Image as the base Image

  3. Set the correct time zone

  4. The program in the Container is started as a non-root user

  5. Specify the interface of the Web program

  6. 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:

The production process should be integrated into the project construction process

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.

Use the official Image as the base Image

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:

  • openjdk:-slim

  • openjdk:-alpine

Regarding Generally speaking, just specify the larger version number. For example, you can write this in the Dockerfile:

FROM openjdk:8-alpine
Copy after login

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.

Set the correct time zone

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

The program in the Container is started as a non-root user

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.

Specify the interface of the web program

For networked applications, the exposed port must be specified in the Dockerfile, otherwise the port cannot be mapped.

EXPOSE 8080
Copy after login

Be able to pass JVM parameters, Java System Properties, and program-defined parameters

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.

Sample project

The sample project source code is here: https://github.com/chanjarster/dockerfile-examples/

Directory structure

All program-related things are stored under /home/java-app/:

/home/java-app
   ├── docker-entrypoint.sh
   ├── lib
   │   └── java-app.jar
   ├── etc
   ├── logs
   └── tmp
Copy after login

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

Method to build Image

mvn clean package dockerfile:build
Copy after login

Run

normal startup, and then visithttp://localhost:8080

docker run -p 8080:8080 chanjarster/dockerfile-java-examples-1:1.0-SNAPSHOT
Copy after login

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

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

Provide program execution Parameters, just add them directly later:

docker run -p 8080:8080 chanjarster/dockerfile-java-examples-1:1.0-SNAPSHOT --debug
Copy after login

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!

Related labels:
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!