其實在docker官網(wǎng)已經(jīng)給出了registry的容器鏡像,我們只需要下載啟動這個容器即可济舆,但為了學(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我們需要注意的是鏡像文件我們究竟放在主機上還是后端專門的服務(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端口上,鏡像文件放在本機的 /var/lib/registry目錄中(一般我們都會用一個單獨的硬盤來掛載這個目錄)罢杉,這里我們直接啟動服務(wù)趟畏,然后講鏡像推送過來。
但是docker的客戶端默認(rèn)是使用https來連接的這里我們可以采用兩種方法滩租。(1)修改docker配置文件允許非安全的鏈接赋秀,將https改成http的。(2)使用nginx反代律想,只需要在nginx上配置ssl即可猎莲。
這里我們實驗就采用第一種方式
目前很多文章都是通過修改docker的配置文件“etc/systemconfig/docker”,重啟docker來解決這個問題技即。但發(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
但是這樣任何都可以訪問我們的倉庫而叼,這時我們就可以用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端口,所以等會我們需要將distribution的端口改為5050葵陵。
http:
addr: :5050
但結(jié)果報錯,看信息應(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"
推測是因為nginx認(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
接下來我們改變排錯思路莹弊,不使用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
然后我們再開啟認(rèn)證考抄,一步一步找出問題所在
登陸
[root@node2 nginx]# docker login -u tom 192.168.31.201:80
Password:
Login Succeeded
push一個鏡像
[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的配置添加到了主配置文件中细疚。