user1578872
user1578872

Reputation: 9028

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?

Upvotes: 2

Views: 6631

Answers (4)

Piotr Jastrząbek
Piotr Jastrząbek

Reputation: 1

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>

Upvotes: 0

gjambet
gjambet

Reputation: 369

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)

Upvotes: 0

Yilmaz Guleryuz
Yilmaz Guleryuz

Reputation: 9745

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

Upvotes: 5

Meiram Chuzhenbayev
Meiram Chuzhenbayev

Reputation: 906

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

Upvotes: 0

Related Questions