其实在docker官网已经给出了registry的容器镜像,我们只需要下载启动这个容器即可,但为了学习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我们需要注意的是镜像文件我们究竟放在主机上还是后端专门的服务器又或者是在云端上。
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协议监听在5000端口上,镜像文件放在本机的 /var/lib/registry目录中(一般我们都会用一个单独的硬盘来挂载这个目录),这里我们直接启动服务,然后讲镜像推送过来。
但是docker的客户端默认是使用https来连接的这里我们可以采用两种方法。(1)修改docker配置文件允许非安全的链接,将https改成http的。(2)使用nginx反代,只需要在nginx上配置ssl即可。
这里我们实验就采用第一种方式
目前很多文章都是通过修改docker的配置文件“etc/systemconfig/docker”,重启docker来解决这个问题。但发现docker1.13.1版本并无此文件,通过查找网上资料,发现
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认证。
先修改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监听了5000端口,所以等会我们需要将distribution的端口改为5050。
http:
addr: :5050
但结果报错,看信息应该是返回的头部信息有无效的字符。
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认证的问题,遂使用nginx添加basic认证
安装basic认证所需要的httpd-tools
yum install httpd-tools -y
htpasswd -c -m /etc/nginx/.ngxpasswd tom
登陆认证
[root@node2 nginx]# docker login -u tom http://192.168.31.201:5000/
Password:
Login Succeeded
但又出现新的问题
[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"
查看仓库镜像已经推送过来了。
[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。
结果镜像能成功上传。
[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
然后我们再开启认证,一步一步找出问题所在
登陆
[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
奇怪的是现在一切正常了
只是将nginx的配置添加到了主配置文件中。