Search code examples
mavendockerjib

Dockerizing multi module Spring Boot application using JIB plugin


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?


Solution

  • 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