Home > Java > javaTutorial > How to use the jib plug-in to build an image of a Java application

How to use the jib plug-in to build an image of a Java application

王林
Release: 2023-05-09 21:07:06
forward
1141 people have browsed it

Introduction

Containerization of Java application development requires the production of docker images. If the user does not have docker installed locally, he can use jib to create a tar file with an image.

Jib provides four ways to use:

  • Maven plug-in: jib-maven-plugin

  • Gradle plug-in: jib -gradle-plugin

  • Java library: Jlib Core

  • Jib CLI

jib build tool It mainly contains four powerful functions.

  • build: Provides the function of creating an image and pushing it to a remote warehouse.

  • buildTar: Provides the function of creating a tar file containing an image.

  • dockerBuild: Provides the function of creating a docker image locally.

  • exportDocker: rContext provides the function of creating dockerfile.

When compiling and building without a docker environment, you cannot use the build command and dockerBuild command to create an image. You can only use the buildTar command to create a tar file containing the image. .

Use the Maven plug-in to build Java applications as docker images

1. Modify the project code that needs to be mirrored: find the pom file to declare the jib plug-in, and
declare the Jib plug-in in the pom.xml file :

<!--使用jib插件-->
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>1.3.0</version>
                <configuration>
                    <!--from节点用来设置镜像的基础镜像,相当于Docerkfile中的FROM关键字-->
                    <from>
                        <!--使用openjdk官方镜像,tag是8-jdk-stretch,表示镜像的操作系统是debian9,装好了jdk8-->
                        <image>openjdk:8-jdk-stretch</image>
                    </from>
                    <to>
                        <!--镜像名称和tag,使用了mvn内置变量${project.version},表示当前工程的version-->
                        <image>lendea/hellojib:${project.version}</image>
                    </to>
                    <!--容器相关的属性-->
                    <container>
                        <!--jvm内存参数-->
                        <jvmFlags>
                            <jvmFlag>-Xms256m</jvmFlag>
                            <jvmFlag>-Xmx256m</jvmFlag>
                        </jvmFlags>
                        <!--要暴露的端口-->
                        <ports>
                            <port>8081</port>
                        </ports>
                    </container>
                </configuration>
            </plugin>
Copy after login
  • from tag: Set the base image, which is equivalent to the FROM keyword in the dockerfile. It is recommended to use the image in SWR. The download speed is fast and stable during construction.

  • to tag: Set the image name and tag of the created image.

  • container tag: Set container-related properties, jvm memory parameters, ports, etc.

2. Create a build task and execute it

# -Dmaven.test.skip=true: 跳过单元测试
# -U: 每次构建检查以来更新,可避免缓存中快照版本依赖不更新问题,但会牺牲部分性能
# -e -X: 打印调试信息,定位疑难构建问题时建议使用此参数构建
# -B: 以batch模式运行,可避免日志打印出现ArrayIndexOutOfBoundsException异常

mvn compile jib:buildTar -Dmaven.test.skip=true -U -e -X -B
Copy after login

In the target directory of the java project, you can see the generated tar image.

3. Use tar image
Execute the docker load -i xxx.tar command to load the tar file image into the local image warehouse, and then use docker run --rm -p 8081:8081 lendea/hellojib:0.0.1 and other commands start the container to test the function.

The above is the detailed content of How to use the jib plug-in to build an image of a Java application. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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