0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Docker] プライベートレジストリを構築して社内開発者にマスタイメージを配布できるようにする

Posted at

概要

社内サーバーにプライベートDockerレジストリを構築する機会があったのでメモ

手順

1. Dockerの導入

必要パッケージのインストール

以下のコマンドで、dnf-plugins-coreをインストールします。

dnf install -y dnf-plugins-core

公式Docker-CEリポジトリの登録

Docker公式のリポジトリを追加します。

dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

Dockerのインストール

次に、Dockerエンジンと関連パッケージをインストールします。

dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Dockerの起動と有効化

Dockerサービスを起動し、システム起動時に自動起動するよう設定します。

systemctl start docker
systemctl enable docker

2. Dockerレジストリの導入

Dockerの公式レジストリイメージを利用して、プライベートレジストリをセットアップします。

レジストリコンテナの起動

以下のコマンドで、ポート5000番でレジストリを起動します。

docker run -d -p 5000:5000 --name registry registry:2.7

レジストリの動作確認

レジストリコンテナが正常に起動しているか確認します。

docker ps

また、ブラウザやcurlを使用して、以下のURLにアクセスし、応答を確認します。

curl http://[サーバーのIPアドレス]:5000/v2/_catalog

エラーがなければ、空のJSONレスポンスが返ってきます。
※ { } とだけ返ってくるはず


3. Dockerイメージのプライベートレジストリへの登録

1. タグの設定

リポジトリにプッシュする前に、イメージにタグを付ける必要があります。

書式:

docker tag <元のイメージ名>:<元のタグ> <レジストリ>:<ポート>/<イメージ名>:<タグ>

例えば、以下のようにタグを付けます。

docker tag my-image:latest <server-ip>:5000/my-image:latest

2. リポジトリへプッシュ

タグ付けしたイメージをレジストリに登録します。

docker push <server-ip>:5000/my-image:latest

3. 登録確認

プライベートレジストリに登録されたイメージを確認するには、以下のコマンドを実行します。

curl http://<server-ip>:5000/v2/_catalog

登録済みのイメージリストが表示されます。


4. Dockerクライアントからの利用

1. docker-compose.ymlの作成

Dockerクライアントからプライベートレジストリのイメージを利用するために、docker-compose.ymlを作成します。

services:
  my_server:
    image: <server-ip>:5000/my-image:latest
    container_name: <container-name>
    restart: unless-stopped
    environment:
      - TZ=Asia/Tokyo
    privileged: true
    command: /sbin/init

2. コンテナのビルドと起動

以下のコマンドを実行し、コンテナをビルド・起動します。

docker-compose up -d --build

ビルドが成功し、コンテナが起動すれば完了 🚀

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?