LoginSignup
0
2

More than 3 years have passed since last update.

コンテナ型仮想化技術 Study02 / Docker レジストリ

Last updated at Posted at 2020-01-06

はじめに

後々、Kubernetes環境でコンテナを実行することを想定すると、レジストリにイメージをPushしておく必要があります。
DockerのレジストリとしてはデフォルトだとDocker HubというDocker社が提供してくれているレジストリが使われますし、個別にローカルにレジストリサーバーを構築することも可能です。
今回は以下のDockerレジストリについて試してみます。

(1) Docker Hub / ホスティングサービス
(2) GitLab.com / ホスティングサービス
(3) GitLab Container Registory / ローカル

関連記事

コンテナ型仮想化技術 Study01 / Docker基礎
コンテナ型仮想化技術 Study02 / Docker レジストリ
コンテナ型仮想化技術 Study03 / Docker Compose
コンテナ型仮想化技術 Study04 / Minikube & kubectl簡易操作
コンテナ型仮想化技術 Study05 / Pod操作
コンテナ型仮想化技術 Study06 / ReplicaSet, Deployment, Service
コンテナ型仮想化技術 Study06' / Kubernetesネットワーク問題判別
コンテナ型仮想化技術 Study07 / ストレージ
コンテナ型仮想化技術 Study08 / Statefulset, Ingress
コンテナ型仮想化技術 Study09 / Helm

参考情報

GitHub.com・BitBucket.org・GitLab.comの月額料金比較 + α
GitLab Container Registry
Vagrant Gitlab Server
Configure Container Registry under an existing GitLab domain
Gitlab × Docker × Container Registry の有効化手順のメモ
GitLab Container RegistryをKubernetesで利用するためのトークン入りSecretの作り方

(1) Docker Hub

以下のDockerHubのサイトからアカウントを作成しておきます。
Docker Hub
今回、tomotagworkというIDを作成しました。

※ちなみに、Docku Hubはアカウントを作れば、無料でイメージの登録(Push)が行えるようです。Privateのリポジトリを作成できる数で有償版の値段が違っているようですが、Publicであれば特に制限は無さそうです。登録されているイメージの利用(Pull)はアカウント無しでもOKです。
参考: Private Repository Pricing

ローカルに作成したイメージに、リモート登録用の名前でタグ付けし、Docker Hubへpushしてみます。

やりたいこと: ローカルでカスタマイズした、example/echo:latestというDockerイメージをtomotagwork/echo:latest というイメージ名でDocker HubにPushする!

vagrant@ubuntu-xenial:~/docker_test/test01$ sudo docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
example/echo        latest              915772b6eca2        9 minutes ago       750MB
example/echo        0.1.0               b128bbc227f5        4 weeks ago         750MB
example/echo        0.0.1               1bab7e1d9051        5 weeks ago         750MB
hello-world         latest              fce289e99eb9        10 months ago       1.84kB
ibm-wex-ee          12.0.2.417          68b237b78230        12 months ago       5.17GB
golang              1.9                 ef89ef5c42a9        15 months ago       750MB
gihyodocker/echo    latest              3dbbae6eb30d        22 months ago       733MB

まず、目的のイメージ"example/echo:latest"に、Pushしたい名前でタグ付けします(tomotagwork/echo:latest)。

vagrant@ubuntu-xenial:~/docker_test/test01$ sudo docker image tag example/echo:latest tomotagwork/echo:latest

vagrant@ubuntu-xenial:~/docker_test/test01$ sudo docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
example/echo        latest              915772b6eca2        15 minutes ago      750MB
tomotagwork/echo    latest              915772b6eca2        15 minutes ago      750MB
example/echo        0.1.0               b128bbc227f5        4 weeks ago         750MB
example/echo        0.0.1               1bab7e1d9051        5 weeks ago         750MB
hello-world         latest              fce289e99eb9        10 months ago       1.84kB
ibm-wex-ee          12.0.2.417          68b237b78230        12 months ago       5.17GB
golang              1.9                 ef89ef5c42a9        15 months ago       750MB
gihyodocker/echo    latest              3dbbae6eb30d        22 months ago       733MB

"tomotagwork/echo:latest"というのができました。IMAGE IDがexample/echo:latestと一致しているので、実体は同じものを指しています。こいつをPushします。

Docker Hubにログインします。

vagrant@ubuntu-xenial:~/docker_test/test01$ sudo docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: tomotagwork
Password:
WARNING! Your password will be stored unencrypted in /home/vagrant/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

Push!

vagrant@ubuntu-xenial:~/docker_test/test01$ sudo docker push tomotagwork/echo:latest
The push refers to repository [docker.io/tomotagwork/echo]
33edad86d155: Pushed                                                                                                    78a4fd59b82f: Pushed                                                                                                    186d94bd2c62: Mounted from library/golang                                                                               24a9d20e5bee: Mounted from library/golang                                                                               e7dc337030ba: Mounted from library/golang                                                                               920961b94eb3: Mounted from library/golang                                                                               fa0c3f992cbd: Mounted from library/golang                                                                               ce6466f43b11: Mounted from library/golang                                                                               719d45669b35: Mounted from library/golang                                                                               3b10514a95be: Mounted from library/golang                                                                               latest: digest: sha256:8f85cd3da96fae8952f54fa22002fda2d4c3b5cb78244a8a90f398892d32ebb9 size: 2417

Push完了です。ブラウザからDocker Hubのサイトにアクセスして確認してみます。
Docker HubのサイトからSiginInすると、以下の画面になって、Pushしたイメージが表示されました。
image.png

image.png

Docker Hubにログインしていない状態でも、以下から参照できた。
https://hub.docker.com/r/tomotagwork/echo
image.png

んー、でも名前で検索しても引っかかってこないんだよなー。なぜだろう???
image.png

とりあえず、これをPullできるか試してみます。
シェル上でも一旦ログアウト

vagrant@ubuntu-xenial:~/docker_test/test01$ sudo docker logout
Removing login credentials for https://index.docker.io/v1/

ローカルのイメージを削除(念のため、コピー元になったexample/echo等も削除しておきます。)

vagrant@ubuntu-xenial:~/docker_test/test01$ sudo docker image rm tomotagwork/echo:latest
Untagged: tomotagwork/echo:latest
Untagged: tomotagwork/echo@sha256:8f85cd3da96fae8952f54fa22002fda2d4c3b5cb78244a8a90f398892d32ebb9

vagrant@ubuntu-xenial:~/docker_test/test01$ sudo docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        10 months ago       1.84kB
ibm-wex-ee          12.0.2.417          68b237b78230        12 months ago       5.17GB

Pullします!

vagrant@ubuntu-xenial:~/docker_test/test01$ sudo docker image pull tomotagwork/echo:latest
latest: Pulling from tomotagwork/echo
55cbf04beb70: Pull complete                                                                                                                                            1607093a898c: Pull complete                                                                                                                                            9a8ea045c926: Pull complete                                                                                                                                            d4eee24d4dac: Pull complete                                                                                                                                            9c35c9787a2f: Pull complete                                                                                                                                            8b376bbb244f: Pull complete                                                                                                                                            0d4eafcc732a: Pull complete                                                                                                                                            186b06a99029: Pull complete                                                                                                                                            34b2359a1f92: Pull complete                                                                                                                                            3e3b0c5acf7d: Pull complete                                                                                                                                            Digest: sha256:8f85cd3da96fae8952f54fa22002fda2d4c3b5cb78244a8a90f398892d32ebb9
Status: Downloaded newer image for tomotagwork/echo:latest

vagrant@ubuntu-xenial:~/docker_test/test01$ sudo docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomotagwork/echo    latest              915772b6eca2        About an hour ago   750MB
hello-world         latest              fce289e99eb9        10 months ago       1.84kB
ibm-wex-ee          12.0.2.417          68b237b78230        12 months ago       5.17GB

Pullできることが確認できました!

※後日、Docker Hubの検索を試してみると、ログインしていない状態でもPushしたイメージを検索することができました。
検索できるようになるまでにはタイムラグがある???
image.png

(2) GitLab.com

参考: GitLab Container Registry

以下のDockerHubのサイトからアカウントを作成しておきます。
GitLab.com

まずプロジェクトを作成します。以下からCreate Projectを選択。
image.png

Blank Projectとして名前だけ指定してCreate!
※プロジェクト名はPushしたいdocker imageのリポジトリ名に合わせる
image.png

左側のメニューの Pakcages - Container Registoryを選択
image.png

まだ何もImageが登録されていないので、Quick Startガイドが表示されます。
image.png

さて、以下のtomotagwork/echo(Docker HubにPushしたやつ)を、別名つけてGitLabにPushしてみます。

vagrant@ubuntu-xenial:~$ sudo docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomotagwork/echo    latest              915772b6eca2        9 days ago          750MB
hello-world         latest              fce289e99eb9        10 months ago       1.84kB
ibm-wex-ee          12.0.2.417          68b237b78230        12 months ago       5.17GB

Pushしたい名前でタグ付けします。

vagrant@ubuntu-xenial:~$ sudo docker image tag tomotagwork/echo:latest registry.gitlab.com/tomotagwork/echo:latest
vagrant@ubuntu-xenial:~$ sudo docker image ls
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
tomotagwork/echo                       latest              915772b6eca2        9 days ago          750MB
registry.gitlab.com/tomotagwork/echo   latest              915772b6eca2        9 days ago          750MB
hello-world                            latest              fce289e99eb9        10 months ago       1.84kB
ibm-wex-ee                             12.0.2.417          68b237b78230        12 months ago       5.17GB

GitLabにログインします。

vagrant@ubuntu-xenial:~$ sudo docker login registry.gitlab.com
Username: tomotagwork
Password:
WARNING! Your password will be stored unencrypted in /home/vagrant/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

Push!

vagrant@ubuntu-xenial:~$ sudo docker push registry.gitlab.com/tomotagwork/echo:latest
The push refers to repository [registry.gitlab.com/tomotagwork/echo]
33edad86d155: Pushed
78a4fd59b82f: Pushed
186d94bd2c62: Pushed
24a9d20e5bee: Pushed
e7dc337030ba: Pushed
920961b94eb3: Pushed
fa0c3f992cbd: Pushed
ce6466f43b11: Pushed
719d45669b35: Pushed
3b10514a95be: Pushed
Put https://registry.gitlab.com/v2/tomotagwork/echo/blobs/uploads/e82877fe-19aa-4eba-acd0-8129d26f9550?_state=tJoWSVU-l84PLWZxOQul84iCiz0JFVcumBOwJD8CNTR7Ik5hbWUiOiJ0b21vdGFnd29yay9lY2hvIiwiVVVJRCI6ImU4Mjg3N2ZlLTE5YWEtNGViYS1hY2QwLTgxMjlkMjZmOTU1MCIsIk9mZnNldCI6NjI4MywiU3RhcnRlZEF0IjoiMjAxOS0xMS0xMVQwMDozNjo0MVoifQ%3D%3D&digest=sha256%3A915772b6eca2726dc2439b154fe4b62960e3124a1c1f21ad92c94b29f8d37ddf: dial tcp: lookup registry.gitlab.com on 10.0.2.3:53: read udp 10.0.2.15:38055->10.0.2.3:53: i/o timeout

あれ?なんかタイムアウトした。
もう一回やってみる。

vagrant@ubuntu-xenial:~$ sudo docker push registry.gitlab.com/tomotagwork/echo:latest
The push refers to repository [registry.gitlab.com/tomotagwork/echo]
33edad86d155: Layer already exists
78a4fd59b82f: Layer already exists    
186d94bd2c62: Layer already exists    
24a9d20e5bee: Layer already exists    
e7dc337030ba: Layer already exists    
920961b94eb3: Layer already exists    
fa0c3f992cbd: Layer already exists    
ce6466f43b11: Layer already exists    
719d45669b35: Layer already exists    
3b10514a95be: Layer already exists    
latest: digest: sha256:8f85cd3da96fae8952f54fa22002fda2d4c3b5cb78244a8a90f398892d32ebb9 size: 2417

OKかな?

GitLabのサイトをブラウザで見てみると...
image.png

デフォルトだとプロジェクトの設定がPrivateになっていたので、Publicに変えてみる。
image.png

一旦シェル上でもログアウト。

vagrant@ubuntu-xenial:~$ sudo docker logout registry.gitlab.com
Removing login credentials for registry.gitlab.com

ローカルのイメージを削除しておきます。(念のため元のImageも削除します。)

vagrant@ubuntu-xenial:~$ sudo docker image rm registry.gitlab.com/tomotagwork/echo:latest tomotagwork/echo:latest
Untagged: registry.gitlab.com/tomotagwork/echo:latest
Untagged: registry.gitlab.com/tomotagwork/echo@sha256:8f85cd3da96fae8952f54fa22002fda2d4c3b5cb78244a8a90f398892d32ebb9
Untagged: tomotagwork/echo:latest
Untagged: tomotagwork/echo@sha256:8f85cd3da96fae8952f54fa22002fda2d4c3b5cb78244a8a90f398892d32ebb9
Deleted: sha256:915772b6eca2726dc2439b154fe4b62960e3124a1c1f21ad92c94b29f8d37ddf
Deleted: sha256:7af40439c11f5e4388b727c1479fcfeefb72e8bd2957e3b59c02633043748ed0
Deleted: sha256:0b7c9201981c2664c28b545c5512add300b1c141e443166b8e8e10d8aa4ebd07
Deleted: sha256:17390723275513e7505aae3369480402f214a8114cac79966639cbc7ed14f7e6
Deleted: sha256:d676491c2e24f03e941c718c3eca48ef400a6e801b20ee6519d5e064b2afcfa6
Deleted: sha256:14027861f16bb2c874c1212e8969e90e70e32b8d31e59db69048308596f841d1
Deleted: sha256:549d1efa00654bcaa5e91e449192ad71557b694bf8a810c4b8d9df0246a00164
Deleted: sha256:2d9c829ae3f7ff3e148e5c7c3a1cf378b0f90b79035e2fe9a8d78c63ccde4c89
Deleted: sha256:b1ae7168c6f3e061aa3943740ec3ceaf8e582dc65feab31d2b56d464a5062d59
Deleted: sha256:4a495dbc04bd205c728297a08cf203988e91caeafe4b21fcad94c893a53d96dc
Deleted: sha256:3b10514a95bec77489a57d6e2fbfddb7ddfdb643907470ce5de0f1b05c603706

先ほどPushしたImageをPullしてみます。

vagrant@ubuntu-xenial:~$ sudo docker image pull registry.gitlab.com/tomotagwork/echo:latest
latest: Pulling from tomotagwork/echo
55cbf04beb70: Pull complete     
1607093a898c: Pull complete             
9a8ea045c926: Pull complete             
d4eee24d4dac: Pull complete             
9c35c9787a2f: Pull complete             
8b376bbb244f: Pull complete             
0d4eafcc732a: Pull complete             
186b06a99029: Pull complete             
34b2359a1f92: Pull complete             
3e3b0c5acf7d: Pull complete             
Digest: sha256:8f85cd3da96fae8952f54fa22002fda2d4c3b5cb78244a8a90f398892d32ebb9
Status: Downloaded newer image for registry.gitlab.com/tomotagwork/echo:latest

Pullできました。

vagrant@ubuntu-xenial:~$ sudo docker image ls
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
registry.gitlab.com/tomotagwork/echo   latest              915772b6eca2        10 days ago         750MB
hello-world                            latest              fce289e99eb9        10 months ago       1.84kB
ibm-wex-ee                             12.0.2.417          68b237b78230        12 months ago       5.17GB

(3) GitLab Container Registry

ローカルの環境にRegistryのサーバーを立てます。ここではGitLabに含まれるContainer Registry機能を使います。

GitLab環境構築

VagrantでVirtualBox上にGitLabサーバー用のGuestOSを立ててみることにします。
以下のようなものがあったので、これを元に、Vagrantファイルとスクリプトを作成します。
https://gist.github.com/cjtallman/b526d8c7d8b910ba4fd41eb51cd5405b

Windows上に適当なフォルダを掘って(c:\y\Vagrant\vagrant-gitlab)、その下に以下のファイルを作成します。

Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
    config.vm.box = "ubuntu/xenial64"
    config.vm.hostname = "gitlab.local.dev"

    config.vm.network :private_network, ip: "172.16.30.10"
    config.vm.network :forwarded_port, guest: 80, host: 8080

    config.vm.provider "virtualbox" do |vb|
        vb.name = "GitLab Server"
        vb.memory = "3072"
        vb.cpus = "3"
    end

    config.vm.provision :docker
    config.vm.provision "shell", path: "install_gitlab.sh"
end

ipアドレスは環境に合わせて変更。

install_gitlab.sh
echo ==== Installing Requirements ==============================================
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y curl openssh-server ca-certificates postfix

echo ==== Installing GitLab CE =================================================
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
sudo apt-get install -y gitlab-ce
sudo gitlab-ctl reconfigure
sudo gitlab-ctl status

#echo ==== Installing GitLab Multi Runner =======================================
#curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash
#sudo apt-get install -y gitlab-ci-multi-runner

Multi Runnerはとりあえずいらなそうなのでコメントアウト。

サーバー作成します。

C:\y\Vagrant\vagrant-gitlab> vagrant up

(やたら長いので省略)
...
    default:
    default: Running handlers:
    default: Running handlers complete
    default:
    default: Chef Client finished, 545/1453 resources updated in 30 minutes 01 seconds
    default: gitlab Reconfigured!
    default: run: alertmanager: (pid 16782) 3s; run: log: (pid 15718) 1662s
    default: run: gitaly: (pid 16705) 7s; run: log: (pid 15142) 1778s
    default: run: gitlab-exporter: (pid 16750) 5s; run: log: (pid 15593) 1678s
    default: run: gitlab-workhorse: (pid 16729) 6s; run: log: (pid 15496) 1703s
    default: run: grafana: (pid 16804) 2s; run: log: (pid 16674) 29s
    default: run: logrotate: (pid 15527) 1693s; run: log: (pid 15542) 1690s
    default: run: nginx: (pid 15508) 1699s; run: log: (pid 15520) 1696s
    default: run: node-exporter: (pid 16738) 6s; run: log: (pid 15575) 1686s
    default: run: postgres-exporter: (pid 16792) 3s; run: log: (pid 15802) 1654s
    default: run: postgresql: (pid 15258) 1774s; run: log: (pid 15274) 1771s
    default: run: prometheus: (pid 16760) 5s; run: log: (pid 15687) 1666s
    default: run: redis: (pid 15096) 1787s; run: log: (pid 15104) 1784s
    default: run: redis-exporter: (pid 16753) 5s; run: log: (pid 15620) 1674s
    default: run: sidekiq: (pid 15460) 1711s; run: log: (pid 15470) 1710s
    default: run: unicorn: (pid 16557) 36s; run: log: (pid 15450) 1714s

30分くらいかかりました。CPU/ディスクアクセスの負荷が結構長い時間高かったです。

※当初、Memory:2GB CPU:2core の割り当てでしたが、GitLabは結構メモリ食うらしく、2GBだとカツカツっぽいです。そのため最初の構成時にSwapしまくってCPU/Diskに負荷がかかっていたと思われます。その後、Memory:3GB, CPU:3coreに変更しました。上のVagrantfileは修正済み。これで割と安定して動いてるっぽい。GitLabって結構重たいのね...。Container Registryとしてだけ使うにはかなりオーバースペックなのかも。

ゲストOSの80番ポートをホストOS側には8080番でポートフォワードしているので、ゲストOS(Windows)のブラウザから、http://localhost:8080にアクセスしてみます。
image.png
接続できました。

パスワード変更を促されているので変更します。これはrootユーザーのパスワードかな?
パスワード変更後、以下の画面になったので、rootユーザーで先ほどのパスワード指定してみると...
image.png

Sign inできました。
image.png

念のため、minikubeの環境もあげて、そちらからhttp://172.16.30.10:80 にアクセスできることを確認しておきます。

vagrant@minikube:~$ curl http://172.16.30.10
<html><body>You are being <a href="http://172.16.30.10/users/sign_in">redirected</a>.</body></html>

OKぽいです。

Container Registryの有効化

さて、次に、Container Registryを有効化しておく必要があります。上の手順では、GitLabをOmnibus package installation (https://about.gitlab.com/install/) という方法でインストールしていることになりますが、どうやらデフォルトではContainer Registryは有効化されていません。

以下のガイドに従って有効化します。
https://docs.gitlab.com/ce/administration/packages/container_registry.html#configure-container-registry-under-an-existing-gitlab-domain

以下の設定をファイルの末尾に追記します。

/etc/gitlab/gitlab.rb
...
registry_external_url 'https://gitlab.example.com:4567'

DNS使えないので、gitlab.example.comというホスト名をGitLabサーバーの/etc/hostsに設定します。

/etc/hosts
127.0.0.1       localhost gitlab.example.com

こいつを使う想定のdocker側にもhostsを設定して名前解決できるようにしておきます。今回はminikubeから使う想定なので、minikubeのサーバーの/etc/hostsに以下の設定をしておきます。

172.16.30.10    gitlab.example.com

dockerコマンドを使ってRegistryにアクセスする際、どうやらSSL通信を使わないといけないようなので、証明書の設定をしておく必要があります。正式な証明書を有償で入手するのはしんどいので、自己署名証明書(いわゆるオレオレ証明書)を設定しておきます。
証明書とキーはそれぞれ、デフォルトだと/etc/gitlab/ssl/gitlab.example.com.crtおよび/etc/gitlab/ssl/gitlab.example.com.keyファイルが想定されるようなので、この名前で作成します。
オレオレ証明書の作り方は以下を参考に。有効期限は3650日(10年)を指定します。
https://docs.docker.com/registry/insecure/#deploy-a-plain-http-registry

vagrant@gitlab:~$ sudo mkdir /etc/gitlab/ssl
vagrant@gitlab:~$ sudo openssl req -newkey rsa:4096 -nodes -sha256 -keyout /etc/gitlab/ssl/gitlab.example.com.key -x509 -days 3650 -out /etc/gitlab/ssl/gitlab.example.com.crt
Generating a 4096 bit RSA private key
..............................................................................................++
...................................................................................................................................................................................................++
writing new private key to '/etc/gitlab/ssl/gitlab.example.com.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:JP
State or Province Name (full name) [Some-State]:Chiba
Locality Name (eg, city) []:Chiba
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:gitlab.example.com
Email Address []:

CountryNameなど聞かれるので、上のように指定しました。
結果、以下のように証明書が作成されました。

vagrant@gitlab:~$ ls -la /etc/gitlab/ssl/
total 16
drwxr-xr-x 2 root root 4096 Dec  5 07:51 .
drwxrwxr-x 4 root root 4096 Dec  5 07:48 ..
-rw-r--r-- 1 root root 2029 Dec  5 07:51 gitlab.example.com.crt
-rw-r--r-- 1 root root 3272 Dec  5 07:51 gitlab.example.com.key

GitLabの構成変更したので、reconfigureを実行。

vagrant@gitlab:~$ sudo gitlab-ctl reconfigure
...(省略)
Running handlers:
Running handlers complete
Chef Client finished, 49/808 resources updated in 39 seconds
gitlab Reconfigured!

vagrant@gitlab:~$ netstat -an | grep 4567
tcp        0      0 0.0.0.0:4567            0.0.0.0:*               LISTEN

registry用に指定した4567ポートをListenするようになりました。

push/pull操作を試す

PublicのクラウドサービスのGitLabでやってみたことを、ローカルに作成したGitLabでもやってみます。

ユーザー作成
image.png

Create a projectを選択
image.png

hello-world用のプロジェクトを作成します。Visibility LevelはInternalにしてみます。
image.png

左側のメニューの Pakcages - Container Registoryを選択
image.png

まだ何もImageが登録されていないので、Quick Startガイドが表示されます。
image.png

さてさて、それではいよいよ、minikubeが乗ってるサーバーのdocker環境から、このGitLabのregistryにアクセスしてみます。

vagrant@minikube:~$ docker login gitlab.example.com:4567
Username: tomotaglocal
Password:
Error response from daemon: Get https://gitlab.example.com:4567/v2/: x509: certificate signed by unknown authority

ログインしようとするとエラーになりました。恐らく証明書を検証できないから。GitLab側で作成した自己署名証明書を、利用するdocker環境に持ってきます。
/etc/docker/certs.d以下に、レジストリのサーバー名のディレクトリを作成してそこに配置します。

vagrant@minikube:~$ sudo mkdir /etc/docker/certs.d
vagrant@minikube:~$ sudo mkdir /etc/docker/certs.d/gitlab.example.com:4567

上のディレクトリに証明書のファイルを配置して確認

vagrant@minikube:~$ sudo ls -la /etc/docker/certs.d/gitlab.example.com:4567
total 12
drwxr-xr-x 2 root root 4096 Dec  5 08:36 .
drwxr-xr-x 3 root root 4096 Dec  5 08:25 ..
-rwxr-xr-x 1 root root 2029 Dec  5 08:36 gitlab.example.com.crt

再度ログインしてみると、うまくいきました!

vagrant@minikube:~$ docker login gitlab.example.com:4567
Username: tomotaglocal
Password:
WARNING! Your password will be stored unencrypted in /home/vagrant/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

公式DockerHubからhello-worldをpullし、ローカルのGitLabのRegistry用にタグ付けしてpushしてみます。

vagrant@minikube:~$ sudo docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
Digest: sha256:4df8ca8a7e309c256d60d7971ea14c27672fc0d10c5f303856d7bc48f8cc17ff
Status: Image is up to date for hello-world:latest

vagrant@minikube:~$ sudo docker tag hello-world gitlab.example.com:4567/tomotaglocal/hello-world

vagrant@minikube:~$ sudo docker image ls | grep hello
hello-world                                        latest              fce289e99eb9        11 months ago       1.84kB
gitlab.example.com:4567/tomotaglocal/hello-world   latest              fce289e99eb9        11 months ago       1.84kB

vagrant@minikube:~$ sudo docker push gitlab.example.com:4567/tomotaglocal/hello-world
The push refers to repository [gitlab.example.com:4567/tomotaglocal/hello-world]
af0b15c8625b: Pushed
latest: digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a size: 524

GitLabの状況をブラウザで見てみると、pushされているのが分かります。
image.png

一旦docker環境から削除します。

vagrant@minikube:~$ sudo docker image rm gitlab.example.com:4567/tomotaglocal/hello-world hello-world
Untagged: gitlab.example.com:4567/tomotaglocal/hello-world:latest
Untagged: gitlab.example.com:4567/tomotaglocal/hello-world@sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a
Untagged: hello-world:latest
Untagged: hello-world@sha256:4df8ca8a7e309c256d60d7971ea14c27672fc0d10c5f303856d7bc48f8cc17ff
Untagged: hello-world@sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3

Logoutした状態でGitLabからpullしてみます。

vagrant@minikube:~$ sudo docker logout gitlab.example.com:4567
Removing login credentials for gitlab.example.com:4567

vagrant@minikube:~$ sudo docker image pull gitlab.example.com:4567/tomotaglocal/hello-world
Using default tag: latest
Error response from daemon: Get https://gitlab.example.com:4567/v2/tomotaglocal/hello-world/manifests/latest: denied: access forbidden

アクセス権が無いと怒られた。想定通り。
Loginしてからpullします。

vagrant@minikube:~$ docker login gitlab.example.com:4567
Username: tomotaglocal
Password:
WARNING! Your password will be stored unencrypted in /home/vagrant/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

vagrant@minikube:~$ sudo docker image pull gitlab.example.com:4567/tomotaglocal/hello-world
Using default tag: latest
latest: Pulling from tomotaglocal/hello-world
1b930d010525: Pull complete
Digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a
Status: Downloaded newer image for gitlab.example.com:4567/tomotaglocal/hello-world:latest

vagrant@minikube:~$ sudo docker image ls | grep hello
gitlab.example.com:4567/tomotaglocal/hello-world   latest              fce289e99eb9        11 months ago       1.84kB

pull成功!

これで、ローカルに構築したGitLabのRegistry使って、ImageのPush/Pullが確認できました!

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2