Jib으로 Spring Boot 애플리케이션 컨테이너화

이 게시물에서는 Docker 클라이언트를 설치하지 않고 Dockerfile을 사용하지 않고 Spring Boot 애플리케이션용 Docker 또는 OCI 호환 이미지를 생성하는 방법에 대해 알아봅니다.

우리는 Jib 일명 Java Image Builder의 도움으로 이 모든 작업을 수행할 것입니다.

지브란?



Jib is a Java containerizer from Google that lets Java developers build containers using build tools like Maven and Gradle.


그러나 이것이 Jib의 흥미로운 전부는 아닙니다. Docker 설치, Dockerfile 유지 관리 등에 대해 알 필요가 없습니다.

개발자는 생성할 아티팩트(jar, war 등)에만 관심이 있으며 Docker의 넌센스(빌드/푸시 등)를 처리할 필요가 없습니다.



와우, 이것은 정말 강력합니다! 하지만 어떻게?

지브하는 방법



Jib을 사용하면 Maven 또는 Gradle 플러그인을 pom.xml.or build.gradle 파일에 추가하여 Java 애플리케이션을 즉시 컨테이너화할 수 있습니다. 그렇게 간단합니다. 그러나 이 게시물에서는 데모 목적으로 Maven을 다룰 것입니다. 그럼 시작하겠습니다.

Springinitializr을 사용하여 작동하는 Spring Boot 프로젝트를 생성할 것입니다. Spring Boot 애플리케이션의 소스 코드를 사용할 수 있으며here 이미지가 Jib를 통해 푸시되고 이미지가 Docker를 통해 실행될 때 "Hello"메시지만 출력합니다.

IDE를 사용할 준비가 되면 다음 단계를 진행할 수 있습니다.

메이븐 설정



<plugin>
   <groupId>com.google.cloud.tools</groupId>
   <artifactId>jib-maven-plugin</artifactId>
   <version>2.6.0</version>
   <configuration>
      <from>
         <image>gcr.io/distroless/java:11</image>
      </from>
      <to>
         <image>registry.hub.docker.com/hiashish/spring-boot-jib-image</image>
      </to>
   </configuration>
</plugin>

For Maven, you can paste the above content in your pom.xml plugin section, and you are good to go. But I will try to explain from and image tags here.

<from> Configures the base image to build your application on top of. Typically you don’t need to provide the from tag as, by default, it uses a distroless Java 8 image. However, I have used Java 11, so I have explicitly mentioned that here. Moreover, depending upon your use case, you may want to use a different base image.
<image> This refers to the target image that will pushed to the container registry. I have used the Docker registry, but you can use any cloud provider(ECS, GCR, ACR) container registry.

To use further options with the plugin, you can refer to the documentation.

레지스트리에 대한 자격 증명 설정

To push an image, we would need to add registry credentials to maven settings.xml. Since we are just doing a demo, it’s ok to provide credentials this way but avoid using it as it is not secure. You may want to secure credentials as mentioned here.

<server>
    <id>registry.hub.docker.com</id>
    <username>username</username>
    <password>password</password>
</server>

이미지 구축

To build an image, we can do it in the following ways.

  1. IDE. For example, in IntelliJ, you can go to maven view of your project, then go under Plugins>jib, then right-click and run the maven build. You may want to create an IntelliJ run configuration that can run maven goals like clean, compile, etc., then push your image.
  2. Using the command line. Just run the below command to build an image of your application. Make sure you have Maven installed first:
    mvn compile jib:build
    It will compile, build, and then push your application's image to the configured container registry.

Following is the output:

ashish@MacBook-Air springboot % mvn compile jib:build
[INFO] Scanning for projects…
[INFO]
[INFO] — — — — — — — — — — < com.example:spring-boot-jib > — — — — — — — — — — -
[INFO] Building springboot 0.0.1-SNAPSHOT
[INFO] — — — — — — — — — — — — — — — — [ jar ] — — — — — — — — — — — — — — — — -
[INFO]
[INFO] — — maven-resources-plugin:3.1.0:resources (default-resources) @ spring-boot-jib — -
[INFO] Using ‘UTF-8’ encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] — — maven-compiler-plugin:3.8.1:compile (default-compile) @ spring-boot-jib — -
[INFO] Nothing to compile — all classes are up to date
[INFO]
[INFO] — — jib-maven-plugin:2.6.0:build (default-cli) @ spring-boot-jib — -
[WARNING] ‘mainClass’ configured in ‘maven-jar-plugin’ is not a valid Java class: ${start-class}
[INFO]
[INFO] Containerizing application to registry.hub.docker.com/hiashish/spring-boot-jib-image…
[WARNING] Base image ‘gcr.io/distroless/java:11’ does not use a specific image digest — build may not be reproducible
[INFO] Using credentials from Maven settings file for registry.hub.docker.com/hiashish/spring-boot-jib-image
[INFO] Using base image with digest: sha256:b25c7a4f771209c2899b6c8a24fda89612b5e55200ab14aa10428f60fd5ef1d1
[INFO]
[INFO] Executing tasks:
[INFO]
[INFO] Executing tasks:
[INFO]
[INFO] Executing tasks:
[INFO] [======= ] 25.0% complete
[INFO]
[INFO] Executing tasks:
[INFO] [======= ] 25.0% complete
[INFO] > pushing blob sha256:6508f436f385b3751366f90b6…
[INFO]
[INFO] Executing tasks:
[INFO] [======= ] 25.0% complete
[INFO] > pushing blob sha256:6508f436f385b3751366f90b6…
[INFO] > pushing blob sha256:c5e22041fc97b838b93a2e18d…
[INFO]
[INFO] Executing tasks:
[INFO] [======= ] 25.0% complete
[INFO] > pushing blob sha256:6508f436f385b3751366f90b6…
[INFO] > pushing blob sha256:c5e22041fc97b838b93a2e18d…
[INFO] > pushing blob sha256:b25902383f9ee26808b68ca62…
[INFO]
[INFO] Executing tasks:
[INFO] [======= ] 25.0% complete
[INFO] > pushing blob sha256:6508f436f385b3751366f90b6…
[INFO] > pushing blob sha256:c5e22041fc97b838b93a2e18d…
[INFO] > pushing blob sha256:b25902383f9ee26808b68ca62…
[INFO] > checking base image layer sha256:31eb28996804…
[INFO]
[INFO] Executing tasks:
[INFO] [======== ] 27.8% complete
[INFO] > pushing blob sha256:c5e22041fc97b838b93a2e18d…
[INFO] > pushing blob sha256:b25902383f9ee26808b68ca62…
[INFO] > checking base image layer sha256:31eb28996804…
[INFO]
[INFO] Executing tasks:
[INFO] [========= ] 30.6% complete
[INFO] > pushing blob sha256:c5e22041fc97b838b93a2e18d…
[INFO] > checking base image layer sha256:31eb28996804…
[INFO]
[INFO] Executing tasks:
[INFO] [========== ] 33.3% complete
[INFO] > checking base image layer sha256:31eb28996804…
[INFO]
[INFO] Executing tasks:
[INFO] [=========== ] 35.0% complete
[INFO]
[INFO] Executing tasks:
[INFO]
[INFO]
[INFO]
[INFO] Container entrypoint set to [java, -cp, /app/resources:/app/classes:/app/libs/*, com.jib.example.spring.SpringbootApplication]
[INFO]
[INFO] Built and pushed image as registry.hub.docker.com/hiashish/spring-boot-jib-image
[INFO] Executing tasks:
[INFO] [=========================== ] 91.7% complete
[INFO] > launching layer pushers
[INFO]
[INFO] — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
[INFO] BUILD SUCCESS
[INFO] — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
[INFO] Total time: 8.746 s
[INFO] Finished at: 2020–11–16T02:34:33+05:30
[INFO] — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

이미지 실행

We have successfully pushed the image (image name:spring-boot-jib-image) to a Docker registry. Now we can run the image using Docker.

As you can see that our application is running inside a container. Now just run the curl command, and you can see that we got a hello message from our containerized spring-boot application.

curl localhost:8080/hello
Hello From Spring-Boot Jib

빠른 데모





결론



이 기사에서는 Docker 없이 Java 애플리케이션을 컨테이너화하는 방법을 배웠습니다. 또한 Jib를 사용하면 Docker를 사용하여 이미지를 빌드할 수 있지만 X 요소는 아닙니다.

Java 애플리케이션에 Jib을 사용하는 다른 이점으로는 Java 애플리케이션과 통합하기가 매우 쉽고 더 빠른 빌드 생성, 재현 가능한 빌드, 커뮤니티 지원 등이 있습니다.

Jib의 이점에 대해 자세히 알아보려면 이link를 참조하십시오.

지원 해줘



방금 읽은 내용이 마음에 들면 커피를 사주세요! :-)


추가 자료



이전articles의 일부를 계속 읽을 수 있습니다.

좋은 웹페이지 즐겨찾기