Docker Registry使用:公有Docker Registry使用、私有Docker Registry的搭建

公有Docker Registry的操做

首先必須註冊本身的dockerhub帳號,假設爲simpledockerhubhtml

[root@localhost ]# docker login --默認即https://hub.docker.comnode

Username : simpledockerhublinux

Password: *****docker

Login Succeededjson

[root@localhost ]# docker pull hello-worldcentos

[root@localhost ]# docker tag hello-world simpledockerhub/hello-world瀏覽器

[root@localhost ]#docker push simpledockerhub/hello-world     ------注意 /前面的名稱必須是用戶註冊的用戶名。服務器

這樣就把hello-world鏡像上傳到simpledockerhub用戶下,使用docker pull命令就能夠下載該鏡像了dom

私有Docker Registry的搭建

1. 單機版:只能經過localhost操做(我的玩玩還行)

[root@localhost ]#  docker run -d -p 5000:5000 registry:2測試

[root@localhost ]#  docker tag hello-world localhost:5000/hello-world

[root@localhost ]#  docker push localhost:5000/hello-world

2.經過自簽名證書方式聯機訪問(生產環境推薦使用該方案):

在主機上安裝一個自簽名的證書,並同時給須要訪問寄存服務器的每一個Docker 守護進程都安裝一份。

Registry主機地址:10.76.64.63,  訪問者地址:10.76.64.82

Registry主機10.76.64.63作以下操做:

1. 建立目錄,存放證書文件:

[root@localhost ]# mkdir centos1_certs

2. 生成自簽名證書,拷貝到 /etc/docker/certs.d/centos1:5000目錄

[root@centos1 ~]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout centos1_certs/domain.key -x509 -days 365 -out centos1_certs/domain.crt
。。。
Common Name (eg, your name or your server's hostname) []:centos1    ---這個爲主機名稱,須要和/etc/hosts裏對應上
。。。

[root@centos1 ~]# mkdir -p /etc/docker/certs.d/centos1:5000

[root@centos1 ~]# cp centos1_certs/domain.crt /etc/docker/certs.d/centos1:5000/domain.crt

3. 修改hosts文件

[root@centos1 ~]#  vi /etc/hosts

10.76.64.63 centos1

4. 修改docker代理服務器文件(若是原來沒有設置能夠跳過)

[root@centos1 ~]# vi /etc/systemd/system/docker.service.d/http-proxy.conf      --------該文件配置見:http://www.javashuo.com/article/p-exhojhzu-hk.html

[Service]
Environment="NO_PROXY=127.0.0.1,localhost,centos1,10.76.*.*"

5. 重啓服務

[root@centos1 ~]# systemctl daemon-reload
[root@centos1 ~]# systemctl restart docker.service

6. 啓動registry 

[root@centos1 ~]# docker run -d --rm -p 5000:5000 --name registry -v /root/centos1_certs:/certs -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key docker.io/registry:2

7. 測試push命令

[root@localhost ]#  docker tag hello-world centos1:5000/hello-world

[root@centos1 ~]# docker push centos1:5000/hello-world
The push refers to repository [centos1:5000/hello-world]
ee83fc5847cb: Pushed
latest: digest: sha256:aca41a608e5eb015f1ec6755f490f3be26b48010b178e78c00eac21ffbe246f1 size: 524

經過瀏覽器調用

  到此爲止能夠經過主機名在本機訪問registry了,如何在另一臺安裝了docker環境的客戶機訪問registry呢?

8. 客戶機10.76.64.82配置

直接訪問:以下,報錯是必然的

[root@centosdocker ~]# docker tag hello-world centos1:5000/hello-world:1.1
[root@centosdocker ~]# docker push centos1:5000/hello-world:1.1
The push refers to repository [centos1:5000/hello-world]
Get https://centos1:5000/v2/: Service Unavailable

解決步驟:

把centos1主機的domain.crt文件上傳到客戶機的centos1_certs目錄,而後執行:

[root@centosdocker ~]# mkdir -p /etc/docker/certs.d/centos1:5000

[root@centosdocker ~]# cp centos1_certs/domain.crt /etc/docker/certs.d/centos1:5000/domain.crt

接下來執行上面的三、四、5步。

如今能夠測試push命令了:

[root@centosdocker ~]# docker tag hello-world centos1:5000/helloworld
[root@centosdocker ~]# docker push centos1:5000/helloworld
The push refers to repository [centos1:5000/helloworld]
ee83fc5847cb: Mounted from hello-world
latest: digest: sha256:aca41a608e5eb015f1ec6755f490f3be26b48010b178e78c00eac21ffbe246f1 size: 524

 成功啦!

3.另外的方案:

對將要訪問寄存服務器的全部Docker 守護進程加上-- insecure-registry ip或hostname:5000 參數,其中的地址和端口須要替換成你的服務器的信息,而後從新啓動Docker 守護進程。

1. [root@centosdocker ~]#vi /etc/hosts

10.76.64.82 centosdocker

2. [root@centosdocker ~]# vi /lib/systemd/system/docker.service

....

OPTIONS='--selinux-enabled --insecure-registry centosdocker:5000'      -----添加這一行
[Install]
WantedBy=multi-user.target

3. [root@centosdocker ~]# vi  /etc/docker/daemon.json

{"insecure-registries":["centosdocker:5000"] }

4. [root@centosdocker system]# systemctl daemon-reload
5. [root@centosdocker system]# systemctl restart docker.service
6. [root@centosdocker system]# docker run -d -p 5000:5000 registry:2

7. [root@centosdocker system]# docker tag hello-world centosdocker:5000/hello-world

8. [root@centosdocker system]# docker push centosdocker:5000/hello-world
The push refers to repository [centosdocker:5000/hello-world]
ee83fc5847cb: Pushed
latest: digest: sha256:aca41a608e5eb015f1ec6755f490f3be26b48010b178e78c00eac21ffbe246f1 size: 524

部署到客戶機除了不須要[root@centosdocker system]# docker run -d -p 5000:5000 registry:2外,其餘操做相同,有興趣能夠試一下,若是有代理的話,須要在docker的no_proxy配置項上添加上registry主機名,如:

[Service]Environment="NO_PROXY=127.0.0.1,localhost,centos1,centosdocker,10.76.*.*"

相關文章
相關標籤/搜索