
1. 环境准备
Linux版本:Centos7docker版本:17.05.0-ce
2. 部署Registry
2.1 使用docker pull命令获取registry的image# sudo docker pull registry:latest
2.2 使用docker run使用下载的registry的image启动一个容器(这里做成脚本registry.sh)
#!/bin/bash echo "restart registry..." docker stop registry && echo "停止服务成功!" || echo "停止服务失败!" docker rm registry && echo "销毁服务成功!" || echo "销毁服务失败!" docker run --detach \ --name registry \ --publish 5000:5000 \ --restart always \ --volume /home/docker/registry/:/var/lib/registry/ \ registry:latest执行registry.sh 私有仓库registry即可允许起来。
2.3. 测试
curl http://localhost:5000/v2/
3.运行Registry Container并使用Nginx做代理安全代理
3.1 安装htpasswd因为需要使用nginx提供安全验证的功能,需要一个地方放置用户名和密码对。
使用由httpd-tools提供的htpasswd工具生成用户名密码对。
安装httpd-tools.
$ yum install httpd-tools
3.2 设置授权账户(账户:docker)
htpasswd -c registry.htpasswd docker
3.3 加入SSL验证
如果你有经过认证机构认证的证书,则直接使用将证书放入nginx目录下即可。如果没有,则使用openssl创建自己的证书。
a) 生成一个新的root key
$ openssl genrsa -out dockerRootCA.key 2048
b) 生成根证书(一路回车即可)
$ openssl req -x509 -new -nodes -key dockerRootCA.key -days 10000 -out dockerRootCA.crt
c) 为server创建一个key。(这个key将被nginx配置文件registry.conf中ssl_certificate_key域引用)
$openssl genrsa -out docker.weipaiku.com.key 2048
d) 为server生产证书
openssl req -new -key docker.weipaiku.com.key -out docker.weipaiku.com.csr
制作证书签名请求。注意在执行下面命令时,命令会提示输入一些信息,”Common Name”一项一定要输入你的域名(docker.weipaiku.com),其他项随便输入什么都可以。不要输入任何challenge密码,直接回车即可。
f) 签署认证请求
$ openssl x509 -req -in docker.weipaiku.com.csr -CA dockerRootCA.crt -CAkey dockerRootCA.key -CAcreateserial -out docker.weipaiku.com.crt -days 10000
3.4.配置nginx使用证书
a)编辑registry.conf
upstream docker-registry { server 192.168.0.210:5000; } server { listen 443; server_name docker.weipaiku.com; # SSL ssl on; ssl_certificate /etc/nginx/conf.d/cert/registry/docker.weipaiku.com.crt; ssl_certificate_key /etc/nginx/conf.d/cert/registry/docker.weipaiku.com.key; # disable any limits to avoid HTTP 413 for large image uploads client_max_body_size 0; # required to avoid HTTP 411: see Issue #1486 (https://github.com/docker/docker/issues/1486) chunked_transfer_encoding on; location /v2/ { # Do not allow connections from docker 1.5 and earlier # docker pre-1.6.0 did not properly set the user agent on ping, catch "Go *" user agents if ($http_user_agent ~ "^(docker\/1\.(3|4|5(?!\.[0-9]-dev))|Go ).*$" ) { return 404; } # To add basic authentication to v2 use auth_basic setting plus add_header auth_basic "registry.docker.yaok.com"; auth_basic_user_file /etc/nginx/conf.d/cert/registry/registry.htpasswd; add_header 'Docker-Distribution-Api-Version' 'registry/latest' always; proxy_pass http://docker-registry; proxy_set_header Host $http_host; # required for docker client's sake proxy_set_header X-Real-IP $remote_addr; # pass on real client's IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_read_timeout 900; } }b)把虚拟主机配置放到nginx配置文件中
把配置文件(registry.conf) 放到nginx的conf.d目录下面,重启即可。
(nginx配置参照:https://blog.csdn.net/shgh_2004/article/details/80422896)
c) 测试
重新nginx 服务,并测试https://docker.weipaiku.com/v2/
4. Docker客户端使用Registry
a) 安装ca-certificates包$ yum install ca-certificates
b) 使能动态CA配置功能
$ update-ca-trust force-enable
c) 将key拷贝到/etc/pki/ca-trust/source/anchors/
$ cp dockerRootCA.crt /etc/pki/ca-trust/source/anchors/
d) 使新拷贝的证书生效
$ update-ca-trust extract
e)证书拷贝后,需要重启docker以保证docker能使用新的证书
$ service docker restart
f)Docker pull/push image测试
制作要push到registry的镜像
5. push 一个实例
a) 从仓库中获取imagedocker pull hello-world:latest
b) 打标签tag
docker tag hello-world:latest docker.weipaiku.com/hello-world:1.0
c) push 到私有仓库
docker push docker.weipaiku.com/hello-world:1.0
文章来源: Docker(2) 私有仓库registry的搭建、nginx集成、与SSL认证人吐槽 | 人点赞 |
发表评论