ksaitoの日記

日々試したことの覚え書き

kanikoでコンテナをビルドする

移転しました。

自動的にリダイレクトします。

kanokoを使ってコンテナをdocker hubにpushします。

kanikoは、kubernetes上で動かしている例が多いのですが、コンテナなのでローカルのdocker環境でお手軽に動くのでは、と思ったのですが、いろいろ落とし穴がありました。

dockerhubへの認証情報

docker loginすると$HOME/.docker/config.jsonに作成されるファイルを使います。

Mac環境だと、credSstoreにkeychainが指定されるため、そのまま使うことはできなかったので下記のようなファイルを準備しました。

****の部分は、echo <dockerhubユーザ>:<dockerhubパスワード> | base64の結果を入れます。

$ cat ~/.ssh/docker.json 
{
        "auths": {
                "https://index.docker.io/v1/": {
                        "auth": "****"
                }
        },
        "HttpHeaders": {
                "User-Agent": "Docker-Client/18.06.1-ce (linux)"
        }
}

Dockerfileの準備

下記のようなDockerfileを準備します。

$ cat Dockerfile
FROM nginx:1.13.12-alpine

COPY static /usr/share/nginx/html
$ cat static/index.html 
<!doctype html>
<meta charset=utf-8>
Hello, World!
$

Dockerhubにregistoryを準備

Dockerhubにログインして適当なregistoryを準備します。(例えば、ユーザ名/hello)

kanikoでビルド&push

$ docker run -v $PWD:/workspace  -v $HOME/.ssh/docker.json:/kaniko/config.json  --env DOCKER_CONFIG=/kaniko  gcr.io/kaniko-project/executor:latest  -d ユーザ名/hello:latest
INFO[0000] Downloading base image nginx:1.13.12-alpine  
INFO[0003] Executing 0 build triggers                   
INFO[0003] Taking snapshot of full filesystem...        
INFO[0003] Skipping paths under /kaniko, as it is a whitelisted directory 
INFO[0003] Skipping paths under /proc, as it is a whitelisted directory 
INFO[0003] Skipping paths under /sys, as it is a whitelisted directory 
INFO[0003] Skipping paths under /dev, as it is a whitelisted directory 
INFO[0003] Skipping paths under /workspace, as it is a whitelisted directory 
INFO[0003] Using files from context: [/workspace/static] 
INFO[0003] COPY static /usr/share/nginx/html            
INFO[0003] Taking snapshot of files...                  
2019/06/14 06:46:06 existing blob: sha256:b430473be128c1302a75e8381dfbaa45182fec81db4f75b749e4896d7816c2ea
2019/06/14 06:46:06 existing blob: sha256:ff3a5c916c92643ff77519ffa742d3ec61b7f591b6b7504599d95a4a41134e28
2019/06/14 06:46:06 existing blob: sha256:7d4e05a01906143afc15671a53151ea9755dac230db376a8b8364b21ca774dbc
2019/06/14 06:46:06 existing blob: sha256:8aeac9a3205fce5e21ab65ccce330fe389a9aaf47e4b682970b1772ae25a1d73
2019/06/14 06:46:07 pushed blob sha256:c05093cd454d3b9b777da1374a0c92a3c2dd2c43819b563299cb49025a943412
2019/06/14 06:46:07 pushed blob sha256:1be502b12f4e82cb3dcf3df03a45489c807b414ab5c0225cffb788ff8fbcdc92
2019/06/14 06:46:08 index.docker.io/ユーザ名/hello:latest: digest: sha256:a15f1ec662f02fc7efa2871a7c4e53fcce3e0da40975d2686ffb11ccc94d2ec6 size: 1072
$