docker創(chuàng)建私人倉庫

其實(shí)在docker官網(wǎng)已經(jīng)給出了registry的容器鏡像,我們只需要下載啟動(dòng)這個(gè)容器即可,但為了學(xué)習(xí)registry的原理這里我們采取最原始的方法下載安裝。

docker search registry
INDEX       NAME                                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/registry                                The Docker Registry 2.0 implementation for...   2410      [OK]       

安裝
yum install -y docker-distribution
registry我們需要注意的是鏡像文件我們究竟放在主機(jī)上還是后端專門的服務(wù)器又或者是在云端上。

vim /etc/docker-distribution/registry/config.yml

version: 0.1
log:
  fields:
    service: registry
storage:
    cache:
        layerinfo: inmemory
    filesystem:
        rootdirectory: /var/lib/registry
http:
    addr: :5000

配置文件是yaml格式的很容易看懂,通過http協(xié)議監(jiān)聽在5000端口上,鏡像文件放在本機(jī)的 /var/lib/registry目錄中(一般我們都會(huì)用一個(gè)單獨(dú)的硬盤來掛載這個(gè)目錄),這里我們直接啟動(dòng)服務(wù),然后講鏡像推送過來。
但是docker的客戶端默認(rèn)是使用https來連接的這里我們可以采用兩種方法。(1)修改docker配置文件允許非安全的鏈接,將https改成http的。(2)使用nginx反代,只需要在nginx上配置ssl即可。
這里我們實(shí)驗(yàn)就采用第一種方式
目前很多文章都是通過修改docker的配置文件“etc/systemconfig/docker”,重啟docker來解決這個(gè)問題。但發(fā)現(xiàn)docker1.13.1版本并無此文件,通過查找網(wǎng)上資料,發(fā)現(xiàn)

vim /etc/docker/daemon.json
#添加下面這段
{"insecure-registries":["192.168.31.201:5000"]}

記得要將鏡像的tag打上registry的地址

[root@node2 nginx]# docker push 192.168.31.201:5000/lvqing.io/mycentos
The push refers to a repository [192.168.31.201:5000/lvqing.io/mycentos]
b59f116f4611: Pushed 
138cc9ab2ba4: Pushed 
683f499823be: Pushed 
latest: digest: sha256:fff04fb3d707ba00ffc5e33a15e49c3a3d9564456ea2c6b471546df732afa695 size: 942

但是這樣任何都可以訪問我們的倉庫,這時(shí)我們就可以用nginx反代來基于basic認(rèn)證。
先修改nginx配置

vim /etc/nginx/conf.d/registry.conf

server {
        listen 5000;
        server_name registry.lvqing.com;
        client_max_body_size 0;

        location / {
            proxy_pass  http://192.168.31.201:5050;
            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";
        }
}

注意這里nginx監(jiān)聽了5000端口,所以等會(huì)我們需要將distribution的端口改為5050。

http:
    addr: :5050

但結(jié)果報(bào)錯(cuò),看信息應(yīng)該是返回的頭部信息有無效的字符。

docker push 192.168.31.201:5000/lvqing.io/nginx:latest 
The push refers to a repository [192.168.31.201:5000/lvqing.io/nginx]
94e060203147: Pushing 6.656 kB
52c5f73e61f7: Pushing 3.072 kB
8c4124960f27: Pushing 13.55 MB/13.55 MB
b4a8d4b3e7a6: Pushing 3.584 kB/295.7 kB
4c54cf2b651e: Pushing 4.627 MB/4.627 MB
5389ee0bb63a: Waiting 
071d8bd76517: Waiting 
error parsing HTTP 405 response body: invalid character '<' looking for beginning of value: "<html>\r\n<head><title>405 Not Allowed</title></head>
\r\n<body bgcolor=\"white\">\r\n<center><h1>405 Not Allowed</h1></center>\r\n<hr><center>nginx/1.12.2</center>\r\n</body>\r\n</html>\r\n"

推測(cè)是因?yàn)閚ginx認(rèn)證的問題,遂使用nginx添加basic認(rèn)證

安裝basic認(rèn)證所需要的httpd-tools
yum install httpd-tools -y

htpasswd -c -m  /etc/nginx/.ngxpasswd tom
登陸認(rèn)證
[root@node2 nginx]# docker login -u tom http://192.168.31.201:5000/
Password: 
Login Succeeded

但又出現(xiàn)新的問題

[root@node2 nginx]# docker push 192.168.31.201:5000/lvqing.io/centos
The push refers to a repository [192.168.31.201:5000/lvqing.io/centos]
071d8bd76517: Pushing 201.8 MB/201.8 MB
Error: Status 404 trying to push repository lvqing.io/centos: "404 page not found\n"

查看倉庫鏡像已經(jīng)推送過來了。

[root@node2 nginx]# tree /var/lib/registry/docker/registry/v2/repositories/lvqing.io/centos/
/var/lib/registry/docker/registry/v2/repositories/lvqing.io/centos/
└── _uploads
    └── 901f864c-fe23-482b-a27a-e099702b941a
        ├── data
        ├── hashstates
        │   └── sha256
        │       └── 0
        └── startedat

接下來我們改變排錯(cuò)思路,不使用5000端口來反代,而是將配置貼在了nginx的主配置文件中,使用80端口來反代docker-distribution。
結(jié)果鏡像能成功上傳。

[root@node2 nginx]# docker push 192.168.31.201:80/newcentos:1.0 
The push refers to a repository [192.168.31.201:80/newcentos]
071d8bd76517: Pushed 
1.0: digest: sha256:365fc7f33107869dfcf2b3ba220ce0aa42e16d3f8e8b3c21d72af1ee622f0cf0 size: 529

然后我們?cè)匍_啟認(rèn)證,一步一步找出問題所在

登陸
[root@node2 nginx]# docker login -u tom 192.168.31.201:80
Password: 
Login Succeeded

push一個(gè)鏡像
[root@node2 nginx]# docker push 192.168.31.201:80/mynginx:1.0 
The push refers to a repository [192.168.31.201:80/mynginx]
94e060203147: Pushed 
52c5f73e61f7: Pushed 
8c4124960f27: Pushed 
b4a8d4b3e7a6: Pushed 
4c54cf2b651e: Pushed 
5389ee0bb63a: Pushed 
071d8bd76517: Mounted from newcentos 
1.0: digest: sha256:7d0285fa26f3258c9aa92b141e73bc1c642723a2d7ee8339984694fe0dd10482 size: 1785

奇怪的是現(xiàn)在一切正常了
只是將nginx的配置添加到了主配置文件中。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容