1. 程式人生 > >使用官方 docker registry 搭建私有映象倉庫及部署 web ui

使用官方 docker registry 搭建私有映象倉庫及部署 web ui

本文介紹本人在 Centos 7.1 上的搭建過程 private docker registry 的全過程,參考自這篇官網文件,英語好的可以直接看官網文件,裡面的內容更詳細,涉及更多原理性的東西,而本文側重於動手實踐。 本文不介紹 docker 的基礎概念,也不介紹為什麼要搭建 private docker registry(這方面的內容網上有太多文件)。並且,雖然可以在好幾種不同的環境中搭建,但本文只介紹在 Centos 7.1 上的搭建過程,其它版本的系統搭建過程,也請查閱上面給出過的官網文件

先安裝 docker 1.6 或更高版本

0. 前提條件

需要 centos 7 64位系統,且核心版本最少為 3.10。可通過下面指令檢查你的核心版本:
$ uname -r
3.10.0-229.14.1.el7.x86_64

1.安裝

安裝 docker 有三種方式:
  • 通過原始碼安裝。又可分為兩種方式,完全通過原始碼安裝和通過 docker 自身完成安裝。前一種方式不僅非常複雜,而且網上介紹的文章很少,這裡不介紹。後一種需要你的主機已經安裝了 docker,這個要求有點先有雞還是先有蛋的意味,這裡也不介紹。
  • 通過 yum 安裝。這種方式最簡單,正是本文要介紹的,它會自動幫你下載安裝各種依賴檔案。
  • 通過 curl 來安裝。這種方式其實是執行一個安裝指令碼,在這個腳本里面也是通過 yum 來安裝的。
通過 yum 安裝的步驟如下: 1.1,使用 root 或其它有 sudo 許可權的賬號登入進系統。以下過程假設你使用 root 登入,如果是其它賬號,請在命令最前面加上 sudo。

1.2,確保你的 yum 包是最新的
$ yum update

1.3,新增 yum 倉庫源
tee /etc/yum.repos.d/docker.repo <<-'EOF'
> [dockerrepo]
> name=Docker Repository
> baseurl=https://yum.dockerproject.org/repo/main/centos/7/
> enabled=1
> gpgcheck=1
> gpgkey=https://yum.dockerproject.org/gpg
> EOF

1.4,安裝 docker 包
$ yum install docker-engine

1.5,啟動 docker 守護程序
$ service docker start

1.6,到這一步安裝 docker 就算完成了,我們可以執行一個 hello-world 容器來驗證一下安裝是否正確
$ sudo docker run hello-world 
 Unable to find image 'hello-world:latest' locally 
        latest: Pulling from hello-world 
        a8219747be10: Pull complete 
        91c95931e552: Already exists 
        hello-world:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security. 
        Digest: sha256:aa03e5d0d5553b4c3473e89c8619cf79df368babd1.7.1cf5daeb82aab55838d 
        Status: Downloaded newer image for hello-world:latest 
        Hello from Docker. 
        This message shows that your installation appears to be working correctly. 

        To generate this message, Docker took the following steps: 
         1. The Docker client contacted the Docker daemon.
         2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (Assuming it was not already locally available.)
         3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
         4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.

         To try something more ambitious, you can run an Ubuntu container with: 
          $ docker run -it ubuntu bash 

         For more examples and ideas, visit: 
          http://docs.docker.com/userguide/


出現上述這樣的輸出,就說明安裝正確了。

2.建立一個 docker 使用者組

docker 守護程序繫結到一個 unix socket 而不是 tcp 埠。預設情況下,這個 unix socket 的所有者是 root,其他使用者需要通過 sudo 的方式訪問。因為 docker 守護程序總是以 root 使用者執行。 為避免使用 docker 命令的時候必須使用 sudo,我們可以建立一個 docker 使用者組,然後將你的使用者新增到這個組裡面。當 docker 守護程序啟動時,它會讓 docker 使用者組對該 unix socket 有可讀可寫的許可權。 當然如果你只會使用 root 賬號登入使用 docker 命令,也可略過這個步驟,對後續程序不會有影響。 建立使用者組並新增使用者的步驟如下: 2.1,使用 sudo 許可權登入進系統 2.2,建立 docker 使用者組
$ sudo groupadd docker

2.3,將你的使用者新增到 docker 組
$ sudo usermod -aG docker your_username

2.4,退出登入然後再登入進來 這確保你的賬號有正確的許可權 2.5,不使用 sudo 來執行 docker 命令,驗證是否正確
$ docker run hello-world
3. 讓 docker 守護程序開機自啟動 為了確保當你重啟系統的時候,docker 可以自動啟動,執行如下指令:

搭建 Docker Registry

以 localhost 執行

要安裝並啟動一個 docker registry 非常簡單,只需要執行下面這條指令:
$ docker run -d -p 5000:5000 --restart=always --name registry registry:2
現在就能夠使用這個 registry 了,下面測試一下。 從 docker 官方映象倉庫獲取一個映象,並使用 tag 指令讓它指向到你的倉庫(這裡我們就以上面下載的 hello-world 映象為例):
$ docker tag hello-world localhost:5000/hello-world:latest
然後將它推送到你的倉庫:
$ docker push localhost:5000/hello-world
然後從本地刪除這個 hello-world 映象:
$ docker rmi  hello-world localhost:5000/hello-world
可以通過命令 docker images 看到 hello-world 映象已經不存在了      然後再從倉庫中把它拉取出來:
$ docker pull localhost:5000/hello-world
可以通過命令 docker images 看到 hello-world 映象又出現了。 可以通過下面的指令停止你的 registry:
$ docker stop registry && docker rm -v registry

儲存

預設情況下,你的 registry 中的資料是儲存在你的主機檔案系統中的一個 docker volume 中的。     如果你想指定這個 docker volume 的儲存位置,以便更容易地訪問 registry 中的資料,可以通過如下指令來完成:
$ docker run -d -p 5000:5000 --restart=always --name registry -v your-path:/var/lib/registry registry:2
當然,也可以將資料儲存在某個遠端檔案系統上,而不是存在本機上,這裡就不介紹了。

遠端訪問

到目前為止,docker registry 已經可以正常使用,且可以指定資料儲存位置。但也只能在本地使用,要想在遠端使用該 registry,就必須使用 TLS 來確保通訊安全,就像使用 SSL 來配置 web 伺服器。也可以強制 docker registry 執行在 insecure 模式,這種模式雖然配置起來要簡單一些,但很不安全,一般不建議使用。 這裡偷懶使用這個簡單的 insecure 模式,假設你在一個域名為 test.docker.midea.registry.hub 的主機上執行 docker registry,步驟如下: 1,在你要遠端訪問 docker registry 的機器上,修改檔案 /etc/default/docker 或 /etc/sysconfig/docker,具體是哪個取決於你的系統。 2,編輯裡面的 DOCKER_OPTS 選項,如果沒有這個選項欄位,就新增一個。改成下面這樣的:
ADD_REGISTRY='--add-registry test.docker.midea.registry.hub:5000'
DOCKER_OPTS="--insecure-registry test.docker.midea.registry.hub:5000"
INSECURE_REGISTRY='--insecure-registry test.docker.midea.registry.hub:5000'
close 配置檔案並儲存修改 3、重啟你的 docker 守護程序,可以使用如下指令來完成:
$ service docker restart
ps: 如果你的系統支援 systemd,也可以使用 systemctl 指令。 通過以上3步,你的這個機器就能遠端從 test.docker.midea.registry.hub 上執行的 docker registry 拉取映象了:
$ docker pull test.docker.midea.registry.hub:5000/hello-world
也可以省略 registry 的域名和埠(會先嚐試從 test.docker.midea.registry.hub 中拉取,失敗後再嘗試從 docker.io 拉取):
$ docker pull hello-world
前提是你的機器要能訪問主機 test.docker.midea.registry.hub,可以修改 /etc/hosts。

部署WebUI

完成了上面的步驟,就可以使用 docker 命令列工具對我們搭建的 registry 做各種操作了,如 push / pull。然而還是不夠方便,比如不能直觀的檢視 registry 中的資源情況,如果有一個 ui 工具,能夠看到倉庫中有哪些映象、每個映象的版本是多少,就好了,下面就介紹 ui 的搭建過程。 這兩個 ui 功能差不多,只需任選其一就可以了。截止到我安裝的時候,docker-registry-frontend 的功能還不完善,沒有刪除映象的功能,只能瀏覽。後一個同時具備 刪除和瀏覽 的功能。這兩個 ui 的搭建過程下面都會講。 參考這裡 1、先安裝 docker-compose 2、從 github 中下載 docker-registry-frontend 原始碼
$ git clone https://github.com/kwk/docker-registry-frontend.git

3、修改配置,預設情況下 docker registry 監聽你主機的 5000 埠,ui 監聽你主機的 8443 埠。而我的主機這兩個埠從外部都不能訪問,所以要分別修改這兩個埠到 10050 和 10080。除了埠,還需要根據你的需要修改倉庫中的映象檔案儲存的位置,我的映象要儲存到 /var/lib/registry。
$ vim docker-compose.yml
# Setup front-end
frontend:
  image: konradkleine/docker-registry-frontend:v2
  #build: ../
  links:
    - registry:registry
  ports:
    # Publish the frontend's port 443 on the IP 0.0.0.0 on port 8443
   *** - "0.0.0.0:10080:443"***
  volumes:
    - ./frontend/certs/frontend.crt:/etc/apache2/server.crt:ro
    - ./frontend/certs/frontend.key:/etc/apache2/server.key:ro
  environment:
    # The front-end is SSL protected
    - ENV_USE_SSL=yes
    - ENV_DOCKER_REGISTRY_HOST=registry
    - ENV_DOCKER_REGISTRY_PORT=5000
    # The registry is SSL protected as well
    - ENV_DOCKER_REGISTRY_USE_SSL=1

# Setup registry
registry:
  image: registry:2
  volumes:
    # Mount the config directory
    - ./registry/config:/etc/docker/registry:ro
    # Make the registry storage persistent (see ./config/config.yml for the path)
    ***- ./registry/storage:/var/lib/registry:rw***
    # Mount our own certificate and key
    - ./registry/certs:/certs:ro
  ports:
    # Publish registry's port 5000 on the IP 0.0.0.0 on port 5000
    ***- "0.0.0.0:10050:5000"***

啟動
$ docker-compose up -d

ps:上面這個指令其實是根據 docker-compose.yml 中的配置啟動兩個容器(fronted 和 registry,這兩個容器是獨立的,通過 REST API 互動資訊),如果在安裝 web ui 前,你的 private docker registry 已經在運行了,也可以去掉上面檔案中的 registry 欄位,但注意要指定對應的ip和埠資訊。 目標是: registry 監聽主機的 10050 埠,ui 監聽主機的 10080 埠。 1、建立工作目錄
$ mkdir -p hyper-docker-registry-web-config/frontend hyper-docker-registry-web-config/registry
$ cd hyper-docker-registry-web-config

2、建一個用於 registry 的配置檔案
$ vim registry/config.yml
version: 0.1
log:
  level: info
  formatter: text
  fields:
    service: registry-srv
    environment: production
storage:
  cache:
    layerinfo: inmemory
  filesystem:
    rootdirectory: /var/lib/registry
  delete:
    # 要在 ui 上能夠刪除映象,enable 的值必須是 true
    enabled: true
http:
  addr: :5000
  debug:
    addr: :5001

3、新建一個用於 ui 的配置檔案
$ vim frontend/config.yml
registry:
  # Docker registry url
  url: http://registry-srv:5000/v2
  # Docker registry fqdn
  name: localhost:10050
  # To allow image delete, should be false
  readonly: false
  auth:
    # Disable authentication
    enabled: false

4、新建一個啟動指令碼
$ vim startup.sh
#! /bin/bash

# registry 監聽主機的 10050 埠,並將映象檔案儲存在主機的 /var/lib/registry/storage 中。
docker run -d -p 10050:5000 --restart=always --name registry-srv -v $(pwd)/registry/:/etc/docker/registry:ro -v /var/lib/registry/storage:/var/lib/registry registry:2

# ui 監聽主機的 10080 埠
docker run -d -p 10080:8080  --name registry-web --link registry-srv -v $(pwd)/frontend/:/conf/:ro hyper/docker-registry-web

5、啟動
$ chmod +x startup.sh
$ ./startup.sh
除了使用 shell 指令碼啟動,這裡其實也可以寫一個 docker-compose.yml 配置檔案,然後使用 docker-compose 工具啟動這兩個容器。 OK,到這裡,倉庫和 ui 

使用方法:

假設上述的部署在 172.20.30.35 這臺機器上進行的。 需要修改【你的機器】上的2個檔案: 1、在 /etc/hosts 中加入:172.20.30.35 docker.midea.registry.hub 2、修改 /etc/sysconfig/docker,新增或修改下面3個欄位的值:
ADD_REGISTRY='--add-registry docker.midea.registry.hub:10050'
DOCKER_OPTS="--insecure-registry docker.midea.registry.hub:10050"
INSECURE_REGISTRY='--insecure-registry docker.midea.registry.hub:10050'

3、然後重啟你的 docker daemon:
$ service docker restart 或 $ systemctl restart docker.service

4、push 映象
$ docker tag your-image docker.midea.registry.hub:10050/your-image
$ docker push docker.midea.registry.hub:10050/your-image

5、pull 映象
$ docker pull docker.midea.registry.hub:10050/your-image
然後就可以使用這個私有registry了。 OVER~~ 參考文章: