Docker 之 私有倉庫搭建

1  概述nginx

本文將介紹兩種方法搭建docker私有倉庫:基於docker-distribution  和  基於 harbor 搭建git

2  基於docker-distribution 的搭建github

docker-distribution用於構建docker倉庫私服,安裝docker-distribution包,在extras倉庫裏web

yum -y install docker-distributiondocker

查看安裝文件json

[root@docker ~]# rpm -ql docker-distributionvim

/etc/docker-distribution/registry/config.yml安全

/usr/bin/registry服務器

/usr/lib/systemd/system/docker-distribution.serviceide

/usr/share/doc/docker-distribution-2.6.2

/usr/share/doc/docker-distribution-2.6.2/AUTHORS

/usr/share/doc/docker-distribution-2.6.2/CONTRIBUTING.md

/usr/share/doc/docker-distribution-2.6.2/LICENSE

/usr/share/doc/docker-distribution-2.6.2/MAINTAINERS

/usr/share/doc/docker-distribution-2.6.2/README.md

/var/lib/registry

[root@docker ~]#

 

查看/etc/docker-distribution/registry/config.yml文件,有基礎的配置,如默認鏡像放置路徑爲rootdirectory: /var/lib/registry

默認不作修改,啓動服務

[root@docker ~]# systemctl start docker-distribution.service

啓動成功後,服務監聽在5000端口

默認docker不支持不安全的http協議,須要在docker的json配置文件寫入 "insecure-registries":[]這個配置項。假設安裝docker-distribution的服務器ip 爲 10.10.10.72,若是可以解析主機名,也能夠寫 主機名:5000,以下

[root@docker ~]# vim /etc/docker/daemon.json

{

  "registry-mirrors": ["https://eyg9yi6d.mirror.aliyuncs.com"],

  "bip":"172.17.0.1/16",

  "insecure-registries":["10.10.10.72:5000"]

}

重啓docker服務,此時該docker能夠把10.10.10.72當作不安全的倉庫使用,會使用http協議鏈接倉庫10.10.10.72

[root@docker ~]# systemctl restart docker.service

把鏡像推送到10.10.10.72這臺倉庫

首先,把要推送到倉庫的鏡像打標籤,以下

[root@docker ~]# docker tag nginx:v1 10.10.10.72:5000/testdis_nginx:v1

而後把打完標籤的鏡像推送到倉庫

[root@docker ~]# docker push 10.10.10.72:5000/testdis_nginx:v1

若是出現以下的報錯,registry有兩個版本的接口,v1和v2,多是docker-ce推送時調用的是v2版本接口,可是docker-distribution不支持版本v2,只支持v1,這裏默認找v2版本。致使出現以下的報錯

[root@docker ~]# docker push 10.10.10.72:5000/testdis_nginx:v1

The push refers to repository [10.10.10.72:5000/testdis_nginx]

Get https://10.10.10.72:5000/v2/: http: server gave HTTP response to HTTPS client

[root@docker ~]#

可是,默認distribution搭建的倉庫沒有安全認證機制,因此能夠藉助nginx來實現安全認證

 

Docker private RegistryNginx反代配置方式:

# client_max_body_size對客戶端上傳的文件大小不作限制

 

vim nginx.conf

        client_max_body_size 0;

 

        location / {

            proxy_pass  http://registrysrvs;

            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;

            proxy_redirect off;

            proxy_buffering off;

            proxy_set_header        Host            $host;

            proxy_set_header        X-Real-IP       $remote_addr;

            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

 

            auth_basic "Docker Registry Service";

            auth_basic_user_file "/etc/nginx/.ngxpasswd";

        }

Docker-distribution配置文件格式詳細信息:

    https://docs.docker.com/registry/configuration/#list-of-configuration-options

distribution沒有web頁面,管理起來不方便,還有一個解決辦法是vmware harbor,路徑爲https://github.com/vmware/harbor  關於harbor的安裝文檔,能夠見連接 https://github.com/vmware/harbor/blob/master/docs/installation_guide.md

 3  基於harbor 倉庫搭建

安裝包:harbor-offline-installer-v1.5.2.tgz 這個包很大,有1G左右

注意,harbor依賴docker-compose,所以要提早安裝好docker-compose,並且不一樣版本的harbor依賴的docker-compose不同,若是沒有安裝,執行

 yum -y install docker-compose

同時也要安裝docker軟件,並啓動docker服務

 yum -y install docker-ce

systemctl start docker.service

 是一個編譯好的包,展開就能夠啓動

    tar xf  harbor-offline-installer-v1.5.2.tgz  -C /usr/local

     cd /usr/local/harbor

vim  /usr/local/harbor/harbor.cfg

#注意,這個配置文件,若是不更改,裏面的選項都是會自動建立,因此要看清楚,包括路徑是否和本地其餘文件有衝突

#主要修改如下三個配置,其餘配置可以使用默認

hostname=docker.ghbsubby.cn

db_password=root123

#管理登陸密碼

harbor_admin_password = Harbor12345

運行install.sh

. /usr/local/harbor/install.sh

安裝過程,[Step 1]: loading Harbor images … 這一步比較慢,須要下載不少鏡像,來實現相關的服務

中止docker harbor服務,經過docker-compose來實現,全部harbor服務會被中止,命令以下

注意,命令必須在/usr/local/harbor/路徑下執行

docker-compose stop

啓動服務

docker-compose start

 可是這裏有個問題,推送打過標籤的鏡像到harbor是遇到了問題

 docker tag  testrun:latest  10.10.10.72/lnmp/testrun:v1

docker push 10.10.10.72/lnmp/testrun:v1

出現報錯,超時了。

目前添加了如下兩個配置

若是是協議上不支持v1和v2,加入配置insecure-registries可能能夠解決,尤爲docker版本是docker而不是docker-ce時能夠解決。

這裏docker沒有實現安全訪問,所以須要把harbor的主機寫入insecure-registries配置段

[root@docker harbor]# vim /etc/docker/daemon.json

{

  "registry-mirrors": ["https://eyg9yi6d.mirror.aliyuncs.com"],

  "bip":"172.17.0.1/16",

  "insecure-registries":["10.10.10.72"]

} 

[root@docker ~]# vim /etc/sysconfig/docker 

OPTIONS='--insecure-registry 10.10.10.72'

推送仍是不成功,估計也跟版本有關係。操做步驟是沒問題的,應該是哪些配置沒啓用。這個問題還在解決中~~

相關文章
相關標籤/搜索