LoginSignup
0
2

More than 3 years have passed since last update.

dockerのプライベートレジストリに作ったコンテナを格納する~EHW2018「MVPをコンテナ化③」

Last updated at Posted at 2018-12-08

概要

このエントリは、「Enterprise "hello, world" 2018 Advent Calendar 2018」の12/7向けのものです。このAdvent Calendarでは、複数個のエントリにまたがる話の流れも鑑みつつ、なるべく1エントリで1つのトピックをカバーできるようにする予定です。

このエントリで記載するトピックは、前回作ったコンテナをローカルのregistryに登録することです。

前提

おことわり

  • このエントリは、インターネットに直接アクセスできる環境を前提としています。インターネットと断絶された世界での運用は、Advent Calendarに余裕があれば、別エントリでまとめてとりあげます。

想定読者

「Enterprise "hello, world" 2018」的なネタとしては、下記のような状況を想定しています。

コンテナは、registryに登録し、そこからコンテナをdocker pull必要がある。

OS

このエントリは、下記の環境でお送りします。

$ cat /etc/os-release | grep PRETTY
PRETTY_NAME="Ubuntu 18.04.1 LTS"
$ uname -a
Linux Ubuntu1804 4.15.0-39-generic #42-Ubuntu SMP Tue Oct 23 15:48:01 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Docker private regitryを作る

以下の前提でprivate registryを作ります。
- 証明書は、自己証明書を使う。(ローカルに構築したいので、Let's encryptは使えないため)

参照先:下記サイトに従っています。

イメージの格納場所を準備する

$ sudo mkdir /var/private-registry

公式サイトのTest an insecure registryに従い、作業を進めます。

ディレクトリを作り、

$ mkdir -p certs

自己証明書を作ります。

$ openssl req   -newkey rsa:4096 -nodes -sha256 -keyout certs/domain.key   -x509 -days 365 -out certs/domain.crt

<details><summary>上記実行中の様子。証明書用にいくつか項目が聞かれるとことは、空白のままEnterキーで大丈夫です。</summary><div>

```shell-session
Generating a 4096 bit RSA private key
........................................................................................................................................................................................++
..................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................++
writing new private key to 'certs/domain.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]:
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:
Email Address []:

dockerデーモンが証明書を参照できるように、出来上がったファイルをコピーしておきます。

$ sudo mkdir -p /etc/docker/certs.d/localhost:5000
$ sudo cp certs/domain.crt /etc/docker/certs.d/localhost:5000/ca.crt

docker-composeを準備する(インストールしていない場合のみ)

docker-composeがインストールされていない場合には、公式サイトに従って導入します。

$ sudo sudo curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose

docker-compose.ymlを用意する

下記のような設定ファイルを用意しました。このエントリで作った証明書ファイルを参照しています。

version: '3'

services:
  registry:
    image: registry:2.6
    container_name: registry
    environment:
      REGISTRY_HTTP_ADDR: 0.0.0.0:443
      REGISTRY_HTTP_TLS_CERTIFICATE: /certs/domain.crt
      REGISTRY_HTTP_TLS_KEY: /certs/domain.key
    volumes:
      - /var/private-registry:/var/lib/registry
      - ./certs:/certs
    ports:
      - 5443:443
    restart: always
    networks:
      - default

起動

docker-compose.ymlが存在するディレクトリで、下記コマンドで起動します。

$ docker-compose up

コンテナのpush, pull

前述のprivate registryに対して、コンテナのpushとpullを行います。

準備

前の買いで作ったコンテナに、タグをつけます。

$ sudo docker tag vanilla-hw:latest localhost:5443/ehw2018/vanilla-hw:latest

push

$ sudo docker push localhost:5443/ehw2018/vanilla-hw
The push refers to repository [localhost:5443/ehw2018/vanilla-hw]
f89b04970ccd: Pushed
273ca3248753: Pushed
08f4f36c9d4b: Pushed
a1688724228c: Pushed
480f0a5f4c03: Pushed
8827d397f439: Pushed
11af704b9028: Pushed
a1c06ce374f4: Pushed
85284e2f6d9e: Mounted from ehw2018/debian-stretch
latest: digest: sha256:cb4d7ec2776b64d08e61627c6bf83fa91787928caa6b6da894d6dcb0315945e5 size: 2198

pull

まず、手元のイメージを消しておきます。

$ sudo docker rmi localhost:5443/ehw2018/vanilla-hw
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
circleci/picard     latest              b1f2c41821a8        11 hours ago        83.8MB
vanilla-hw          latest              c805d3cae8ca        29 hours ago        756MB
debian-stretch      latest              c59c0c11fdb6        2 days ago          258MB
registry            2.6                 2e2f252f3c88        2 months ago        33.3MB

なくなりましたね。この状態でpullします。

$ sudo docker pull localhost:5443/ehw2018/vanilla-hw
Using default tag: latest
latest: Pulling from ehw2018/vanilla-hw
Digest: sha256:cb4d7ec2776b64d08e61627c6bf83fa91787928caa6b6da894d6dcb0315945e5
Status: Downloaded newer image for localhost:5443/ehw2018/vanilla-hw:latest

イメージがpullされました。

$ sudo docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
circleci/picard                     latest              b1f2c41821a8        11 hours ago        83.8MB
vanilla-hw                          latest              c805d3cae8ca        29 hours ago        756MB
localhost:5443/ehw2018/vanilla-hw   latest              c805d3cae8ca        29 hours ago        756MB
debian-stretch                      latest              c59c0c11fdb6        2 days ago          258MB
registry                            2.6                 2e2f252f3c88        2 months ago        33.3MB

実行

$ sudo docker run -it --rm localhost:5443/ehw2018/vanilla-hw
hello, world

以上で、dockerのprivate registryに登録したコンテナをpullして、実行することができました。

まとめ

このエントリでは、「Enterprise "hello, world" 2018 Advent Calendar 2018」(EHW2018)の7日目として、
作ったコンテナをローカルのregistryに登録する
をトピックとして取り上げました。

EHW2018のネタとしては、このあと、このプログラムと同じような目的を持つものを現代的なツール群を用いて、なるべくめんどくさく実現していくことを考えています。

0
2
1

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