Generator-jhipster: GoogleContainerTools/jib

Created on 10 Jul 2018  路  6Comments  路  Source: jhipster/generator-jhipster

Overview of the feature request

now, jhipster use spotify/dockerfile-maven to build docker image.

Jib's Goals

  1. Fast - Deploy your changes fast. Jib separates your application into multiple layers, splitting dependencies from classes. Now you don鈥檛 have to wait for Docker to rebuild your entire Java application - just deploy the layers that changed.

  2. Reproducible - Rebuilding your container image with the same contents always generates the same image. Never trigger an unnecessary update again.

  3. Daemonless - Reduce your CLI dependencies. Build your Docker image from within Maven or Gradle and push to any registry of your choice. No more writing Dockerfiles and calling docker build/push.

Motivation for or Use Case
Related issues or PR
  • [x] Checking this box is mandatory (this is just to show you read everything)
area

Most helpful comment

Good news, I succeed to use jib (with Gradle) to build a docker image.
This is the content of my gradle/docker.gradle

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
    jcenter()
  }
  dependencies {
    classpath "gradle.plugin.com.google.cloud.tools:jib-gradle-plugin:0.9.10"
  }
}

apply plugin: com.google.cloud.tools.jib.gradle.JibPlugin

jib {
  from {
    image = 'openjdk:8-jre-alpine'
  }
  to {
    image = 'jhipstergradlesampleapplication:latest'
  }
  container {
     jvmFlags = ['-Djava.security.egd=file:/dev/./urandom']
     ports = ['8080']
     useCurrentTimestamp = true
  }
}

jibDockerBuild.dependsOn bootJar

After a quick tour the web app seems to be working as expected (no weird message in the logs).
I make the jibDockerBuild task dependsOn bootJar just to take advantage of the copyIntoStatic task present in gradle/profile_(dev,prod).gradle. I agree that usingbootJaris overkill since I don't need/user the executable fat jar with jib but it prevent to recreate a task indocker.gradle` file.

Since the the frontend is present in build/resources/main jib copies into the /app/resources/static directory of the container and then Spring Web serve it as expected.

/app/resources/static # ls -la
total 44
drwxr-xr-x    6 root     root          4096 Jan  1  1970 .
drwxr-xr-x    7 root     root          4096 Jan  1  1970 ..
drwxr-xr-x    2 root     root          4096 Jan  1  1970 app
drwxr-xr-x    4 root     root          4096 Jan  1  1970 content
-rw-r--r--    1 root     root          5430 Jan  1  1970 favicon.ico
drwxr-xr-x    2 root     root          4096 Jan  1  1970 i18n
-rw-r--r--    1 root     root          1058 Jan  1  1970 index.html
-rw-r--r--    1 root     root           779 Jan  1  1970 manifest.webapp
-rw-r--r--    1 root     root           239 Jan  1  1970 robots.txt
drwxr-xr-x    3 root     root          4096 Jan  1  1970 swagger-ui

I'll try with Maven a propose a PR.

All 6 comments

It looks good but it won't work with JHipster, it has no support to build the front-end, and it requires a classpath that probably won't work well with Spring Boot (that second point could perhaps be worked out, but not the first one).
So at the moment I don't think we can use it.

Thanks @anjia0532 - jib would be great for us, but it needs to do more than just "hello world" apps :-)

Good news, I succeed to use jib (with Gradle) to build a docker image.
This is the content of my gradle/docker.gradle

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
    jcenter()
  }
  dependencies {
    classpath "gradle.plugin.com.google.cloud.tools:jib-gradle-plugin:0.9.10"
  }
}

apply plugin: com.google.cloud.tools.jib.gradle.JibPlugin

jib {
  from {
    image = 'openjdk:8-jre-alpine'
  }
  to {
    image = 'jhipstergradlesampleapplication:latest'
  }
  container {
     jvmFlags = ['-Djava.security.egd=file:/dev/./urandom']
     ports = ['8080']
     useCurrentTimestamp = true
  }
}

jibDockerBuild.dependsOn bootJar

After a quick tour the web app seems to be working as expected (no weird message in the logs).
I make the jibDockerBuild task dependsOn bootJar just to take advantage of the copyIntoStatic task present in gradle/profile_(dev,prod).gradle. I agree that usingbootJaris overkill since I don't need/user the executable fat jar with jib but it prevent to recreate a task indocker.gradle` file.

Since the the frontend is present in build/resources/main jib copies into the /app/resources/static directory of the container and then Spring Web serve it as expected.

/app/resources/static # ls -la
total 44
drwxr-xr-x    6 root     root          4096 Jan  1  1970 .
drwxr-xr-x    7 root     root          4096 Jan  1  1970 ..
drwxr-xr-x    2 root     root          4096 Jan  1  1970 app
drwxr-xr-x    4 root     root          4096 Jan  1  1970 content
-rw-r--r--    1 root     root          5430 Jan  1  1970 favicon.ico
drwxr-xr-x    2 root     root          4096 Jan  1  1970 i18n
-rw-r--r--    1 root     root          1058 Jan  1  1970 index.html
-rw-r--r--    1 root     root           779 Jan  1  1970 manifest.webapp
-rw-r--r--    1 root     root           239 Jan  1  1970 robots.txt
drwxr-xr-x    3 root     root          4096 Jan  1  1970 swagger-ui

I'll try with Maven a propose a PR.

And now maven
pom.xml (sorry for the formatting)

 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>${maven-resources-plugin.version}</version>
                <executions>
                <execution>
                        <id>jib-resources</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/classes/static</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>target/www</directory>
                                    <filtering>false</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
</plugin>
<plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>0.9.10</version>
                <configuration>
                    <from>
                        <image>openjdk:8-jre-alpine</image>
                    </from>
                    <to>
                        <image>jhipstersampleapplication</image>
                    </to>
                    <container>
                        <jvmFlags>
                            <jvmFlag>-Djava.security.egd=file:/dev/./urandom</jvmFlag>
                        </jvmFlags>
                        <ports>
                            <port>8080</port>
                        </ports>
                        <useCurrentTimestamp>true</useCurrentTimestamp>
                    </container>
                </configuration>
            </plugin>
$ ./mvnw clean verify jib:dockerBuild
...
[INFO] --- maven-resources-plugin:3.1.0:copy-resources (jib-resources) @ jhipster-sample-application ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 45 resources
[INFO]
[INFO] --- jib-maven-plugin:0.9.10:dockerBuild (default-cli) @ jhipster-sample-application ---
[WARNING] Setting image creation time to current time; your image may not be reproducible.
[INFO]
[INFO] Containerizing application to Docker daemon as jhipstersampleapplication...
[INFO]
[INFO] Getting base image openjdk:8-jre-alpine...
[INFO] Building dependencies layer...
[INFO] Building resources layer...
[INFO] Building classes layer...
[INFO] The base image requires auth. Trying again for openjdk:8-jre-alpine...
[INFO] Retrieving registry credentials for registry.hub.docker.com...
[INFO] Finalizing...
[INFO] Loading to Docker daemon...
[INFO]
[INFO] Container entrypoint set to [java, -Djava.security.egd=file:/dev/./urandom, -cp, /app/resources/:/app/classes/:/app/libs/*, io.github.jhipster.sample.JhipsterSampleApplicationApp]
[INFO]
[INFO] Built image to Docker daemon as jhipstersampleapplication
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:26 min
[INFO] Finished at: 2018-09-17T16:01:35+02:00
[INFO] ------------------------------------------------------------------------

cool!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marcelinobadin picture marcelinobadin  路  3Comments

pascalgrimaud picture pascalgrimaud  路  3Comments

Steven-Garcia picture Steven-Garcia  路  3Comments

DanielFran picture DanielFran  路  3Comments

dronavallisaikrishna picture dronavallisaikrishna  路  3Comments