Docker レジストリを立ててみる

Docker レジストリを立ててみる

Docker レジストリは基本的に SaaS として提供されているものを利用することが多いと思います。例えば Docker Hub や GitHub Container Registry(GHCR)、Amazon Elastic Container Registry(ECR)などです。

今回ちょっとしたデバッグのためにローカルで Docker レジストリを立てる必要があったので、備忘録代わりに手順メモです。

Docker レジストリを立てる #

といっても手順は簡単で、レジストリ用の Docker イメージが公式で提供されているので、これを利用するだけです。

上記ページでは 5000 番ポートでレジストリが起動するようになっていますが、わたしの Mac 環境ではすでに 5000 番ポートが別のプロセスで使用されていたため、5001 番ポートを指定して起動しました。

docker run -d -p 5001:5000 --restart always --name my-registry registry:3

5000 番ポートが使用されているかは以下のコマンドで確認できます。

lsof -i :5000

docker ps でレジストリコンテナが起動していることを確認しておきましょう。

また、レジストリコンテナに対してリクエストしてみます。正常に起動できていれば以下のようなレスポンスが返ってくるはずです。

curl -i http://localhost:5001/v2/

> HTTP/1.1 200 OK
> Content-Length: 2
> Content-Type: application/json
> Docker-Distribution-Api-Version: registry/2.0
>
> {}

では、このレジストリに対してイメージをプッシュしてみましょう。まずは適当なイメージをビルドします。

ここでは、hello-world イメージ(https://hub.docker.com/_/hello-world)を利用します。

docker pull hello-world

docker tag hello-world localhost:5001/my-hello-world:latest

docker push localhost:5001/my-hello-world:latest

プッシュが成功したら、レジストリにイメージが存在することを確認してみましょう。以下のリクエストを送ると、レジストリに存在するデータが返ってきます。

イメージ一覧を確認。

curl http://localhost:5001/v2/_catalog

> {"repositories":["my-hello-world"]}

イメージのタグを確認。

curl http://localhost:5001/v2/\my-hello-world/tags/list

> {"name":"my-hello-world","tags":["latest"]}

レジストリの立ち上げはこれで完了です。

おまけ:レジストリを公開して docker pull してみる #

あとはおまけで、せっかくなのでこのレジストリをグローバルにアクセスできるようにし pull してみましょう。

トンネリングツールを使います。今回は Cloudflare の cloudflared を利用します。(ほかだと ngrok あたりが有名どころだと思います)

もし cloudflared をインストールしていない場合は、下記ページを参考にインストールしてください。Mac の場合は Homebrew でインストールできます。

以下のコマンドでローカルの 5001 番ポートをトンネリングして公開します。

cloudflared tunnel --url localhost:5001

> INF Thank you for trying Cloudflare Tunnel. Doing so, without a Cloudflare account, is a quick way to experiment and try it out. ......
> INF Requesting new quick Tunnel on trycloudflare.com...
> INF +--------------------------------------------------------------------------------------------+
> INF |  Your quick Tunnel has been created! Visit it at (it may take some time to be reachable):  |
> INF |  https://monroe-regulations-rating-policies.trycloudflare.com                              |
> INF +--------------------------------------------------------------------------------------------+
> .........
> ......
> ...

表示された https://monroe-regulations-rating-policies.trycloudflare.com がトンネリングされた URL になります(その都度変更されます)。ではこの URL に対してリクエストしてみましょう。

curl -i https://monroe-regulations-rating-policies.trycloudflare.com/v2/

> /v2/
> HTTP/2 200
> content-type: application/json
> content-length: 2
> cf-ray: 9ef8498f78ef25c6-NRT
> cf-cache-status: DYNAMIC
> server: cloudflare
> docker-distribution-api-version: registry/2.0
>
> {}

docker pull してみましょう。

docker pull monroe-regulations-rating-policies.trycloudflare.com/my-hello-world:latest

docker image ls で、いまプルしたイメージが存在することが確認できます。

docker image ls

> IMAGE                                                                        ID             DISK USAGE   CONTENT SIZE
> hello-world:latest                                                           f9078146db2e       17.1kB         4.77kB
> localhost:5001/my-hello-world:latest                                         f9078146db2e       17.1kB         4.77kB
> monroe-regulations-rating-policies.trycloudflare.com/my-hello-world:latest   5099b89d7666       17.1kB         4.77kB
> registry:3                                                                   8a7c1aae9db5       80.1MB         18.9MB