Jibを使ってdockerhubにイメージをpushする


はじめに

Jibとは

Maven、Gradleのプラグインとして定義し、Dockerデーモンを使わずにDockerイメージを作成、レジストリにプッシュ出来るツール

ハマったところも書きつつ手順を説明する
ハマり1:proxy
ハマり2:認証

環境

OS : Windows
IDE : eclipse
build : Maven
jib-maven-plugin : 1.0.2

前提

dockerhubアカウントがあること
Repositoryを作っておくこと(今回はtestという名前で行った)

適当なMavenプロジェクトを落とす

何でも良い。自分は以下を落とした
https://github.com/spring-guides/gs-rest-service.git

pom.xml編集

pom.xml
<build>
    <plugins>
・・・
        <plugin>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>jib-maven-plugin</artifactId>
            <version>1.0.2</version>
            <configuration>
                <to>
                    <image>registry.hub.docker.com/{dockerhubId}/test</image>
                </to>
            </configuration>
        </plugin>
    </plugins>
</build>

DockerHubリモートレジストリへPush

$ mvn compile jib:build

eclipse的には

で実行

エラー!

[ERROR] Failed to execute goal com.google.cloud.tools:jib-maven-plugin:1.0.2:build (default-cli) on project gs-rest-service: 縺昴?ョ繧医≧縺ェ繝帙せ繝医?ッ荳肴?弱〒縺吶?? (gcr.io): Unknown host 縺昴?ョ繧医≧縺ェ繝帙せ繝医?ッ荳肴?弱〒縺吶?? (gcr.io) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

proxyを通して再度DockerHubリモートレジストリへPush

$ mvn -Dhttps.proxyHost=・・・ -Dhttps.proxyPort=・・・ -Dhttp.proxyHost=・・・ -Dhttp.proxyPort=・・・ compile jib:build
eclipse的には

で実行

エラー!

[ERROR] Failed to execute goal com.google.cloud.tools:jib-maven-plugin:1.0.2:build (default-cli) on project gs-rest-service: Build image failed, perhaps you should set a credential helper name with the configuration '<to><credHelper>' or set credentials for 'registry.hub.docker.com' in your Maven settings: Unauthorized for registry.hub.docker.com/{dockerhubId}/test: 401 Unauthorized
[ERROR] {"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":[{"Type":"repository","Class":"","Name":"{dockerhubId}/test","Action":"pull"},{"Type":"repository","Class":"","Name":"{dockerhuId}/test","Action":"push"}]}]}
[ERROR] -> [Help 1]

認証を設定する

pom.xml
<build>
    <plugins>
・・・
        <plugin>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>jib-maven-plugin</artifactId>
            <version>1.0.2</version>
            <configuration>
                <to>
                    <image>registry.hub.docker.com/{dockerhubId}/test</image>
                    <auth>
                        <username>{dockerhubId}</username>
                        <password>{password}</password>
                    </auth>
                </to>
            </configuration>
        </plugin>
    </plugins>
</build>

再再度DockerHubリモートレジストリへPush

出来た風

[INFO] --- jib-maven-plugin:1.0.2:build (default-cli) @ gs-rest-service ---
[INFO] 
[INFO] Containerizing application to [36m{dockerhubid}/test[0m...
[INFO] Retrieving registry credentials for registry.hub.docker.com...
[INFO] Getting base image gcr.io/distroless/java:8...
[INFO] Building dependencies layer...
[INFO] Building resources layer...
[INFO] Building classes layer...
[INFO] 
[INFO] Container entrypoint set to [java, -cp, /app/resources:/app/classes:/app/libs/*, hello.Application]
[INFO] 
[INFO] Built and pushed image as [36m{dockerhubid}/test[0m
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.279 s
[INFO] Finished at: 2019-03-19T14:48:47+09:00
[INFO] ------------------------------------------------------------------------

dockerhubにも登録されていることを確認

参考リンク

Jib