[Spring Boot] Using JIB to speed up the build time!

Hojun Song ·2024년 1월 6일
0
post-thumbnail

I deployed a Spring Boot project with Docker, enforced Continuous Delivery, and ran into a big problem. The speed and performance of Oracle's VMs were taking too much time to compile and build the spring boot, causing the build to fail in the Github flow.

2024/01/03 15:09:04 Run Command Timeout

While debugging to troubleshoot an issue that was working well locally, but only in a VM deployment, I started to get frustrated with the slow builds, and even though I tried multi-staging and moderately utilizing Docker's cache features, the builds themselves were too slow for a quick fix.

My first idea

  • was to build directly outside of docker, i.e. in a vm environment.
    So, even though it was a docker environment, I installed the JDK on a VM machine to build. Unfortunately, this didn't work out and I found another way.
  1. Caching build images via Kaniko
  2. using JIB to generate images from Github Workflow and importing them from docker-compose.

build.gradle.kts

jib {
    from {
        image = "azul/zulu-openjdk-alpine:21-jre-headless-latest"
    }
    to {
        image = "hojunsong/tripsketch:latest"
    }
    container {
        entrypoint = listOf("java", "-cp", "/app/resources:/app/classes:/app/libs/*", "kr.kro.ApplicationKt")
    }
}

The listof syntax is used in Kotlin, so you'll need to use "[]" in Java.

cd.yml

      - name: Docker Login
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_HUB_USERNAME }}
          password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

Sign up for Docker, get an access token, and log in with secret.

cd.yml

      - name: Build and push Docker image with Jib
        run: |
          ./gradlew jib

Having the github workflow do the building for me has also been beneficial in terms of resources.
https://hub.docker.com/r/hojunsong/tripsketch

  tripsketch:
    build:
      context: .
      dockerfile: tripsketch.dockerfile

- > After the change

  tripsketch:
    image: hojunsong/tripsketch:latest

Reduced build time from 10+ minutes to less than a minute.



Why Jib's Build Speed is Fast

  1. Layer Separation: Jib divides an application into multiple layers. For example, dependencies, resources, and classes are placed in different layers. By doing this, parts that have not changed do not need to be rebuilt, reducing build time.

  2. Caching Mechanism: Jib stores layers used in previous builds in a cache. If a certain layer has not changed in a new build, Jib reuses the cached layer, shortening the build time.

  3. Container Registry Optimization: Jib can directly push the built image to a container registry. In this process, only the necessary layers are pushed, and existing layers are skipped. This reduces data transfer time.

  4. No Need for Docker Daemon: Jib can operate without a Docker daemon. This simplifies the build process and easily integrates into the build pipeline.

  5. Plugin Integration: Integrated with build tools like Maven or Gradle, Jib works well with the existing Java build process and can be used without additional settings or scripts.


I can skip the dockerfile below and save resources on VM.

FROM azul/zulu-openjdk-alpine:21 as build

RUN apk add git && \
    mkdir /tripsketch && \
    git clone --branch main https://github.com/trip-sketch/tripsketch /tripsketch

WORKDIR /tripsketch
RUN ./gradlew build -x test --no-daemon --parallel

profile
A web backend developer, let's share information and problem solving!

0개의 댓글