6 Aralık 2022 Salı

jib plugin

Giriş
Bu bir Google plugin'i. Açıklaması şöyle. Dockerfile yazmaya gerek kalmadan image oluşturmaya yarar.
Jib is a Maven plugin and a Gradle plugin for building container images for your Java applications. It allows you to build container images directly from your Java code, without needing to manually create a Dockerfile. Jib handles all the steps of packaging your application into a container image, including building the application code, adding files to the image, and configuring the image to run the application.
Sunulan goal listesi şöyle
1. build
2. dockerBuild 

1. build Goal
Örnek
Şöyle yaparız.
<build>
  <plugins>
    <plugin>
      <groupId>com.google.cloud.tools</groupId>
      <artifactId>jib-maven-plugin</artifactId>
      <version>2.8.0</version>
      <configuration>
        <to>
          <image>gcr.io/my-project/my-image</image>
        </to>
      </configuration>
    </plugin>
  </plugins>
</build>

> mvn jib:build
2. dockerBuild Goal
Örnek
Açıklaması şöyle. Image'ı local docker'a yükler.
Jib builds optimized Docker OCI images for Java applications. It is an open source project developed by Google. Similar to builldpacks, no Dockerfile needs to be created.

To use Jib, you need to add a maven plugin into your pom.xml file as follows:

<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
    <version>2.6.0</version>
</plugin>
To build the Docker image run the command:

./mvnw compile jib:dockerBuild -Dimage=rest-server-jib:0.0.1
Command to run the image:

docker run -it -p8080:8080 rest-server-jib:0.0.1
Örnek
Şöyle yaparız. jib.to.image ile image ismi belirtilir
mvn clean install jib:dockerBuild -Djib.to.image=spring-docker-demo:v1
Örnek
Şöyle yaparız
<profile>
  <id>jib-push-to-local</id>
  <activation>
    <activeByDefault>false</activeByDefault>
  </activation>
  <build>
    <plugins>
      <plugin>
        <groupId>com.google.cloud.tools</groupId>
        <artifactId>jib-maven-plugin</artifactId>
        <version>2.5.2</version>
        <configuration>
          <from>
            <image>openjdk:8</image>
          </from>
          <container>
            <ports>
              <port>8080</port>
            </ports>
            <format>OCI</format>
          </container>
        </configuration>
        <executions>
          <execution>
            <id>push-custom-tag</id>
            <phase>package</phase>
            <configuration>
              <to>
                <image>betulsahinn/${app.image.name}:${app.image.tag}</image>
              </to>
            </configuration>
            <goals>
              <goal>dockerBuild</goal>
            </goals>
         </execution>
         <execution>
             <id>push-latest-tag</id>
             <phase>package</phase>
             <configuration>
               <to>
                 <image>betulsahinn/${app.image.name}:latest</image>
               </to>
             </configuration>
             <goals>
               <goal>dockerBuild</goal>
             </goals>
           </execution>
         </executions>
       </plugin>
     </plugins>
   </build>
 </profile>
Şöyle yaparız. dockerBuild goal'ün package aşamasında altına yani yerel Docker'a iki tane image push'lar. Birisi belirtilen image tag'ini kullanır diğeri de latest tag'ini kullanır
mvn clean install jib:dockerBuild -P jib-push-to-local -Dapp.image.tag=v2
3. build Goal
Image'ı remote repository'ye yükler

container/jvmFlag Tag
Şöyle yaparız
<!-- for set JAR file inside the container -->
<containerizingMode>packaged</containerizingMode> 
<container>
    <jvmFlags> <!-- jvm flags -->
        <jvmFlag>-Xms512m</jvmFlag>
        <jvmFlag>-Xdebug</jvmFlag>
    </jvmFlags>
    <ports> <!-- allow port when container running -->
        <port>8080</port>
    </ports>
    <format>Docker</format>
</container>
container/ports/ports Tag
Örnek
Şöyle yaparız
<plugin>
  <groupId>com.google.cloud.tools</groupId>
  <artifactId>jib-maven-plugin</artifactId>
  <version>2.5.2</version>
  <configuration>
    <from>
      <image>openjdk:8</image>
    </from>
    <container>
      <ports>
        <port>8080</port>
      </ports>
      <format>OCI</format>
    </container>
  </configuration>
</plugin>
from Tag
Örnek
Şöyle yaparız
<plugin>
  <groupId>com.google.cloud.tools</groupId>
  <artifactId>jib-maven-plugin</artifactId>
  <version>3.3.1</version>
  <configuration>
    <from>
      <image>openjdk:17</image>
    </from>
  </configuration>
</plugin>
Örnek
Şöyle yaparız. Burada işlemci mimarisi belirtiliyor
<from>
  <image>openjdk:17-oracle</image>
  <platforms>
    <platform>
      <architecture>amd64</architecture>
      <os>linux</os>
    </platform>
    <platform>
      <architecture>arm64</architecture>
      <os>linux</os>
    </platform>
  </platforms>
</from>
to Tag
Image ismini belirtir
Örnek
Şöyle yaparız
<plugin>
  <groupId>com.google.cloud.tools</groupId>
  <artifactId>jib-maven-plugin</artifactId>
  <version>2.8.0</version>
  <configuration>
    <to>
      <image>gcr.io/my-project/my-image</image>
    </to>
  </configuration>
</plugin>
Örnek
Şöyle yaparız. Kullanıcı ismi ve şifre ile giriş yapar
to>
  <image>docker.io/abdalrhmanalkraien/energy-tech</image>
  <tags>
    <tag>${image.version}</tag>
    <tag>latest</tag>
  </tags>
  <auth>
    <username>myUserName</username>
    <password>MyPassword</password>
  </auth>
</to>
Örnek
Şöyle yaparız
<profile>
  <id>jib-push-to-dockerhub</id>
  <activation>
    <activeByDefault>false</activeByDefault>
  </activation>
  <build>
    <plugins>
      <plugin>
        <groupId>com.google.cloud.tools</groupId>
        <artifactId>jib-maven-plugin</artifactId>
        <version>2.5.2</version>
        <configuration>...</configuration>
        <executions>
          <execution>
            <id>push-custom-tag</id>
            <phase>package</phase>
            <configuration>
              <to>
                <image>docker.io/betulsahinn/${app.image.name}:${app.image.tag}</image>
              </to>
            </configuration>
            <goals>
              <goal>build</goal>
            </goals>
          </execution>
          <execution>
            <id>push-latest-tag</id>
            <phase>package</phase>
            <configuration>
              <to>
                <image>docker.io/betulsahinn/${app.image.name}:latest</image>
              </to>
            </configuration>
            <goals>
              <goal>build</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</profile>
Şöyle yaparız. package aşamasında docker.io altına yani Docker Hub'a iki tane image push'lar. Birisi belirtilen image tag'ini kullanır diğeri de latest tag'ini kullanır
mvn clean install -P jib-push-to-dockerhub -Dapp.image.tag=v2

Hiç yorum yok:

Yorum Gönder

Local Snapshot Kullanmak

Örnek Şöyle yaparız <repository> <id>snapshot-repository</id> <name>Maven2 Snapshot Repository</name> &l...