///////////////////////////////////////////////////////////////////////////////

    Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

///////////////////////////////////////////////////////////////////////////////

= Build Container Images with Jib
:description: Helidon Jib Guide
:keywords: helidon, guide, docker, jib
:gctools-url: https://github.com/GoogleContainerTools
:jib-url: {gctools-url}/jib
:jib-maven-plugin-url: {gctools-url}/jib/tree/master/jib-maven-plugin
:distroless-url: {gctools-url}/distroless
:distroless-java-url: {distroless-url}/tree/master/java

This guide describes how to build container images for Helidon applications
 using Jib and Maven.

== What You Need

|===
|About 10 minutes
| <<about/03_prerequisites.adoc,Helidon Prerequisites>>
|===

== Creating a Docker Image Using Jib

link:{jib-url}[Jib] is a java tool chain for building Docker images for Java
 applications. It is integrated with Maven and Gradle and uses a
 link:{distroless-url}[distroless] base image to produce small images.

Jib does not require the `docker` command or the Docker daemon, there is no need
 to solve the Docker-in-Docker problem in order to build Docker images as part
 of your continuous integration.

NOTE: The `docker` command is only required for local usage when registering
 images in your local Docker registry.

The example below shows how to build an image and register it in the local
 registry using the `jib-maven-plugin`.

Add the following plugin declaration to your pom.xml:

[source,xml]
----
<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
    <version>0.10.1</version>
    <configuration>
        <to>
            <image>jib-${project.artifactId}</image>
            <tags>
                <tag>${project.version}</tag>
                <tag>latest</tag>
            </tags>
        </to>
        <container>
            <!-- good defaults intended for containers -->
            <jvmFlags>
                <jmxFlag>-server</jmxFlag>
                <jmxFlag>-Djava.awt.headless=true</jmxFlag>
                <jmxFlag>-XX:+UnlockExperimentalVMOptions</jmxFlag>
                <jmxFlag>-XX:+UseCGroupMemoryLimitForHeap</jmxFlag>
                <jmxFlag>-XX:InitialRAMFraction=2</jmxFlag>
                <jmxFlag>-XX:MinRAMFraction=2</jmxFlag>
                <jmxFlag>-XX:MaxRAMFraction=2</jmxFlag>
                <jmxFlag>-XX:+UseG1GC</jmxFlag>
            </jvmFlags>
            <mainClass>${mainClass}</mainClass>
            <ports>
                <port>8080</port>
            </ports>
        </container>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>dockerBuild</goal>
            </goals>
            <phase>package</phase>
        </execution>
    </executions>
</plugin>
----

NOTE: By default, Jib uses link:{distroless-java-url}/[distroless/java] as the
 base image. You can override the default with configuration see the
 link:{jib-maven-plugin-url}#extended-usage[documentation]

[source,bash]
.Package the updated application
----
mvn package
----

[source,bash]
.Run the image
----
docker run --rm -p 8080:8080 jib-helidon-quickstart-se
----

[source,bash]
.Ping the application
----
curl -X GET http://localhost:8080/greet
----

[source,bash]
.Take a look at the image size
----
docker images jib-quickstart-se:latest
----

[source,bash]
----
REPOSITORY          TAG           IMAGE ID      CREATED        SIZE
jib-quickstart-se   latest        384aebda5594  48 years ago   124MB <1>
----
<1> Ignore the fact that it says the image was created 48 years ago. Refer to
 the {jib-url}/blob/master/docs/faq.md#why-is-my-image-created-48-years-ago[Jib
 FAQ] for explanations.

NOTE: the Jib image is smaller because of the use of a distroless base image.
