How to create muttilayer image using maven jib plugin

2.4k views Asked by At

I want to build an image of one of my microservice using maven jib plugin and as I know docker image can be created in multilayer architecture. I was doing this in Dockerfile manually. But not sure how this can be achieved with maven jib plugin Following is plugin with configuration that I am using to build image.

Please guide me to create multilayer image.

<profile>
            <id>jib</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.google.cloud.tools</groupId>
                        <artifactId>jib-maven-plugin</artifactId>
                        <version>1.0.0</version>
                        <configuration>
                            <from>
                                <image>gcr.io/test/java:11</image>
                            </from>
                            <to>
                                <image>aa/${project.artifactId}</image>
                                <tags>
                                    <tag>latest</tag>
                                    <!--suppress MavenModelInspection -->
                                    <tag>Test</tag>
                                </tags>
                            </to>
                            <container>
                                <ports>
                                    <port>7575</port>
                                    <port>9000</port>
                                    <port>9001</port>
                                    <port>9002</port>
                                </ports>
                                <jvmFlags>
                                    <jvmFlag>-Duser.timezone=GMT</jvmFlag>
                                    <jvmFlag>-Dfile.encoding=utf-8</jvmFlag>
                                    <jvmFlag>-XX:MaxRAMFraction=2</jvmFlag>
                                    <jvmFlag>-XX:+UseG1GC</jvmFlag>
                                    <jvmFlag>-XX:+UseStringDeduplication</jvmFlag>
                                </jvmFlags>
                                <volumes>
                                    <volume>/tmp</volume>
                                </volumes>
                            </container>
                        </configuration>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>dockerBuild</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
1

There are 1 answers

0
Chanseok Oh On BEST ANSWER

(First of all, Jib 1.0.0 is super old. As of now, latest is 2.6.0.)

Jib does layering for you, creating an optimized image. That is one of the main points of using Jib: not thinking about Docker but just focusing on writing Java code. Jib's mission is to enable any Java developer with little to no Docker knowledge to easily containerize their Java apps in an optimized way.

FYI, multi-layering is not the only benefit of Jib; there are many other advantages, one of which is strong reproduciblity. For example, when writing a Dockerfile, the order of layers matters, as you will invalidate all the cached "upper" layers and have to rebuild them if an "lower" layer changes. Moreover, rebuilding with Docker usually results in creating new layers (hence a new image) even if your project hasn't really changed, leading to increased build time and wasting storage and network bandwidth all over the place.

If you are still curious and get to know more about these, here are some references:

For completing the answer, Jib has an extension mechanism, and you can further customize layers using the first-party Layer-Filter extension found in the repo. But it would be rare for most people to ever need the filtering extension.


Lastly, no need to add <tag>latest</tag> below. <tags> is for additional tags, and :latest is implied in aa/${project.artifactId}, just as docker push aa/foo implies aa/foo:latest.

 <image>aa/${project.artifactId}</image>
 <tags>
     <tag>latest</tag> <!-- This is unnecessary! -->