Dockerizing multi module Spring Boot application using JIB plugin

本秂侑毒 提交于 2021-01-28 04:34:12

问题


I have a Spring boot Application and using spotify plugin to Dockerize my application.So, I will have a Dockerfile like the below one.

FROM jdk1.8:latest  

RUN mkdir -p /opt/servie

COPY target/service.war /opt/service

ENV JAVA_OPTS="" \
    JAVA_ARGS=""

CMD java ${JAVA_OPTS} -jar /opt/service/service.war ${JAVA_ARGS}

I came across JIB and it looks really cool. But, struggling to get it working.

I added the pom entry below.

<plugin>
        <groupId>com.google.cloud.tools</groupId>
        <artifactId>jib-maven-plugin</artifactId>
        <version>0.9.6</version>
        <configuration>
          <from>
            <image>jdk1.8:latest</image>
          </from>
          <to>
            <image>docker.hub.com/test/service</image>
          </to>
        </configuration>
      </plugin>

mvn compile jib:build

I see the following.

[INFO] Building dependencies layer... [INFO] Building classes layer... [INFO] Building resources layer...

When i run the docker image, it says, Jar file does not exist. I have a multi module maven project and would like to dockerize multiple module on running mvn compile jib:build from the parent pom. Any help on this?


回答1:


Yes indeed. JIB doesn't need Dockerfile or dockerd.

Sharing an example below, you can just copy it into plugins section of your pom.xml

<plugin>  
    <groupId>com.google.cloud.tools</groupId>  
    <artifactId>jib-maven-plugin</artifactId>  
    <version>0.9.7</version>  
    <configuration>  
    <allowInsecureRegistries>true</allowInsecureRegistries>  
    <from>  
        <image>gcr.io/distroless/java</image>  
    </from>  
    <to>  
    <!-- make sure you already have created a project at Google Cloud Platform, see https://cloud.google.com/container-registry/ -->  
        <image>gcr.io/my-gcp-project/${project.artifactId}:${project.version}</image>  
        <credHelper>gcr</credHelper>  
    </to>  
    <container>  
        <jvmFlags>  
            <jvmFlag>-Xms256m</jvmFlag>  
            <jvmFlag>-Xmx512m</jvmFlag>  
            <jvmFlag>-Xdebug</jvmFlag>  
            <jvmFlag>-XX:+UnlockExperimentalVMOptions</jvmFlag>  
            <jvmFlag>-XX:+UseCGroupMemoryLimitForHeap</jvmFlag>  
        </jvmFlags>  
        <mainClass>learnmake.microservices.RunApplication</mainClass>  
        <ports>  
            <port>8080</port>  
            <!-- <port>4000-4004/udp</port> -->  
        </ports>  
        <format>OCI</format>  
        <!-- OR <format>Docker</format> -->  
        <useCurrentTimestamp>true</useCurrentTimestamp>  
      </container>  
    </configuration>  
</plugin>   

for more detailed example, see learnmake-microservices




回答2:


Try to change line COPY in your Dockerfile to

COPY target/service.war /opt/service/service.war

BTW You can use WORKDIR directive in your Dockerfile to use relative path




回答3:


by using the jib maven plugin you don't need to write a docker file, jib build directly the image for you without exposing a dockerfile.

I think that in your pom.xml switching the packaging from 'war' to 'jar'

would be enough to let you run successfully

mvn compile jib:build

(will fail if docker.hub.com/test/service is a private repository, in this case you need either to switch to public or to provide your credentials)




回答4:


jib:build will try to push image to some repo. If you need to build only you need to use mvn jib:dockerBuild

For multimodule project just configure plugin accordingly in child projects like this:

<executions>
    <execution>
        <phase>package</phase>
            <goals>
                <goal>dockerBuild</goal>
            </goals>
        </execution>
</executions>


来源:https://stackoverflow.com/questions/51373718/dockerizing-multi-module-spring-boot-application-using-jib-plugin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!