在CentOS 6上搭建私有的Docker Registry

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在CentOS 6上搭建私有的Docker Registry相关的知识,希望对你有一定的参考价值。

在CentOS 6上搭建私有的Docker Registry v2
Registry概念
Re
gistry是一个无状态的, 高可扩展的服务器端应用程序, 用于存储和分发Docker Image。
依赖安装
1. 安装Docker
要使用Docker Registry, 当然首先要安装Docker。 假设你已经安装好Docker。 没有安装好可以参考官方文档。
2. 安装Docker-compose
Docker-compose是一个非常有用的Docker运行, 管理的工具。 你可以通过定义compose文件, 使用简单的一条命令同时起多个Docker
Container运行不同的服务。 Docker-compose对于开发, 测试, 环境保存以及CI都提供了非常大的便利。
Docker-compose是用Python开发的一个工具, 所以可以用pip直接安装。
1. $ pip install docker-compose
需要注意的是, docker-compose可能对requests module的版本有限制, 而本机上可能安装了更高版本的requests模块, 造成运行时报
错。 可以使用pip-conflict-checker检查版本冲突, 卸载不合适的版本, 重新安装一个合适的版本。
1. $ pip install pip-conflict-checker
2. $ pipconflictchecker
3. $ pip uninstall requests
4. $ pip install requests==2.7.0
实际使用操作中使用pip安装的docker-compose可能在执行时还会报代码有bug。
所以推荐直接从github中下载稳定的release版本安装。
1. $ curl -L https://github.com/docker/compose/releases/download/1.5.2/docker-compose-`uname -s`-`uname -m` >
/usr/local/bin/docker-compose
2. $ chmod +x /usr/local/bin/docker-compose
3. $ ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
3. 安装htpasswd
因为需要使用nginx提供安全验证的功能, 需要一个地方放置用户名和密码对。
使用由httpd-tools提供的htpasswd工具生成用户名密码对。
安装httpd-tools.
1. $ yum install httpd-tools
运行Registry Container并使用Nginx做代理
1. 运行nginxregistry容器
创建一个工作目录, 例如/data/progrmas/docker, 并在该目录下创建docker-compose.yml文件, 将以下docker-compose.yml内容复制粘贴
到你的docker-compose.yml文件中。
内容大致意思为, 基于“nginx:1.9” image运行nginx容器, 暴露容器443端口到host 443端口。 并挂载当前目录下的nginx/目录为容器
的/etc/nginx/config.d目录。
nginx link到registry容器。 基于registry:2 image创建registry容器, 将容器5000端口暴露到host 5000端口, 使用环境变量指明使用/data为根目
录, 并将当前目录下data/文件夹挂载到容器的/data目录。
1. $ mkdir /data/progrmas/docker -p
2. $ cd /data/programs/docker
3. $ mkdir data && mkdir nginx
1. $ cat /data/programs/docker/docker-compose.yml
2. nginx:
3. image: "nginx:1.9"
4. ports:
5. - 443:443
6. links:
7. - registry:registry
8. volumes:
9. - ./nginx/:/etc/nginx/conf.d
10. registry:
11. image: registry:2
12. ports:
13. - 127.0.0.1:5000:5000
14. environment:
15. REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY: /data
16. volumes:
17.
18. - ./data:/data
2. 配置nginx
在nginx目录中创建registry.conf文件配置nginx。 配置nginx与registry的关系, 转发端口, 以及其他nginx的配置选项。 复制, 粘贴如下内容
到你的registry.conf文件中:
1. $ cat /data/programs/docker/nginx/registry.conf
2. upstream docker-registry {
3. server registry:5000;
4. }
5.
6. server {
7. listen 443;
8. server_name myregistrydomain.com;
9.
10. # SSL
11. # ssl on;
12. # ssl_certificate /etc/nginx/conf.d/domain.crt;
13. # ssl_certificate_key /etc/nginx/conf.d/domain.key;
14.
15. # disable any limits to avoid HTTP 413 for large image uploads
16. client_max_body_size 0;
17.
18. # required to avoid HTTP 411: see Issue #1486 (https://github.com/docker/docker/issues/1486)
19. chunked_transfer_encoding on;
20.
21. location /v2/ {
22. # Do not allow connections from docker 1.5 and earlier
23. # docker pre-1.6.0 did not properly set the user agent on ping, catch "Go *" user agents
24. if ($http_user_agent ~ "^(docker\/1\.(3|4|5(?!\.[0-9]-dev))|Go ).*$" ) {
25. return 404;
26. }
27.
28. # To add basic authentication to v2 use auth_basic setting plus add_header
29. # auth_basic "registry.localhost";
30. # auth_basic_user_file /etc/nginx/conf.d/registry.password;
31. # add_header ‘Docker-Distribution-Api-Version‘ ‘registry/2.0‘ always;
32.
33. proxy_pass http://docker-registry;
34. proxy_set_header Host $http_host; # required for docker client‘s sake
35. proxy_set_header X-Real-IP $remote_addr; # pass on real client‘s IP
36. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
37. proxy_set_header X-Forwarded-Proto $scheme;
38. proxy_read_timeout 900;
39. }
40.
41. }
配置文件创建完成后, 回到工作目录执行docker-compose up运行registry和nginx容器。
1. $ docker-compose up
2. Starting docker_registry_1
3. Starting docker_nginx_1
4. Attaching to docker_registry_1, docker_nginx_1
5. registry_1 | time="2016-01-08T11:22:41Z" level=info msg="Starting upload purge in 7m0s" go.version=go1.5.2
instance.id=4c7af230-a76b-4235-9a8d-2e552c2dbab8 version=v2.2.1
6. registry_1 | time="2016-01-08T11:22:41Z" level=warning msg="No HTTP secret provided - generated random secret. This
may cause problems with uploads if multiple registries are behind a load-balancer. To provide a shared secret, fill
in http.secret in the configuration file or set the REGISTRY_HTTP_SECRET environment variable." go.version=go1.5.2
instance.id=4c7af230-a76b-4235-9a8d-2e552c2dbab8 version=v2.2.1
7. registry_1 | time="2016-01-08T11:22:41Z" level=info msg="redis not configured" go.version=go1.5.2
instance.id=4c7af230-a76b-4235-9a8d-2e552c2dbab8 version=v2.2.1
8. registry_1 | time="2016-01-08T11:22:41Z" level=info msg="using inmemory blob descriptor cache" go.version=go1.5.2
instance.id=4c7af230-a76b-4235-9a8d-2e552c2dbab8 version=v2.2.1
9. registry_1 | time="2016-01-08T11:22:41Z" level=info msg="listening on 0.0.0.0:5000" go.version=go1.5.2
instance.id=4c7af230-a76b-4235-9a8d-2e552c2dbab8 version=v2.2.1
10. registry_1 | time="2016-01-08T11:22:49Z" level=info msg="response completed" go.version=go1.5.2
http.request.host="localhost:5000" http.request.id=1455af27-cbf6-4ab2-8f22-4de35d2aa507 http.request.method=GET
http.request.remoteaddr="192.168.42.1:39027" http.request.uri="/v2/" http.request.useragent="curl/7.19.7 (x86_64-
redhat-linux-gnu) libcurl/7.19.7 NSS/3.19.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
http.response.contenttype="application/json; charset=utf-8" http.response.duration=3.108632ms
http.response.status=200 http.response.written=2 instance.id=4c7af230-a76b-4235-9a8d-2e552c2dbab8 version=v2.2.1
执行docker-compose up后。 注意是否有容器启动失败的消息, 如果容器启动失败的消息, 需要检查网络, 是否能从dockerhub上pull
image( 需代理, 或使用使用国内镜像, 使用国内镜像需更改docker-compose.yml文件中image项) 。 也由可能粘贴配置文件错误, 需仔
细检查。
启动后也可以使用docker ps命令查看是否两个容器都正常运行。
1. $ docker ps
2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
3. 28ac3aba9a22 nginx:1.9 "nginx -g ‘daemon of 38 seconds ago Up 37 seconds 80/tcp,
0.0.0.0:443->443/tcp docker_nginx_1
4. 0cddc713022f registry:2 "/bin/registry /etc/ 38 seconds ago Up 37 seconds
127.0.0.1:5000->5000/tcp docker_registry_1
确定docker容器都正常运行后, 用curl 命令验证功能是否正常运行。 使得localhost:5000和localhost:443访问registry都应该返回{}。
1. curl http://localhost:5000/v2/
2. curl http://localhost:443/v2/
使用ctrl-c退出docker-compose, 继续后面的步骤。
3. 添加用户名和密码
在/data/programs/docker/nginx目录下执行下面命令创建用户名和密码对, 如果要创建多个用户名和密码对, 则不是使用“-c“选项。
1. $ htpasswd -c registry.password docker
然后修改Registry.conf文件, 取消下面三行的注释。
1. auth_basic "registry.localhost";
2. auth_basic_user_file /etc/nginx/conf.d/registry.password;
3. add_header ‘Docker-Distribution-Api-Version‘ ‘registry/2.0‘ always;
再次执行docker-compose up运行registry, 这时使用localhost:5000端口访问得到的结果为”{}”,但是使用localhost:443访问
将得到”401 Authorisation Required“的提示。 加入用户名和密码验证才能得到与直接访问registry 5000端口相同的结果。
1. $ curl http://localhost:5000/v2/
2. {}
3.
4. $ curl http://localhost:443/v2/
5. <html>
6. <head><title>401 Authorization Required</title></head>
7. <body bgcolor="white">
8. <center><h1>401 Authorization Required</h1></center>
9. <hr><center>nginx/1.9.9</center>
10. </body>
11. </html>
12. $ curl http://docker:[email protected]:443/v2/
13. {}
4. 加入SSL验证
如果你有经过认证机构认证的证书, 则直接使用将证书放入nginx目录下即可。 如果没有, 则使用openssl创建自己的证书。
1) 进行/data/programs/docker/nginx目录
( 1) 生成一个新的root key
1. $ openssl genrsa -out devdockerCA.key 2048
( 2) 生成根证书( 一路回车即可)
1. $ openssl req -x509 -new -nodes -key devdockerCA.key -days 10000 -out devdockerCA.crt
( 3) 为server创建一个key。 ( 这个key将被nginx配置文件registry.con中ssl_certificate_key域引用)
1. $openssl genrsa -out domain.key 2048
( 4) 制作证书签名请求。 注意在执行下面命令时, 命令会提示输入一些信息, ”Common Name”一项一定要输入你的域名( 官方说IP也行, 但是
也有IP不能加密的说法) , 其他项随便输入什么都可以。 不要输入任何challenge密码, 直接回车即可。
1. $ openssl req -new -key domain.key -out dev-docker-registry.com.csr
2. You are about to be asked to enter information that will be incorporated
3. into your certificate request.
4. What you are about to enter is what is called a Distinguished Name or a DN.
5. There are quite a few fields but you can leave some blank
6. For some fields there will be a default value,
7. If you enter ‘.‘, the field will be left blank.
8. -----
9. Country Name (2 letter code) [XX]:
10. State or Province Name (full name) []:
11. Locality Name (eg, city) [Default City]:
12. Organization Name (eg, company) [Default Company Ltd]:
13. Organizational Unit Name (eg, section) []:
14. Common Name (eg, your name or your server‘s hostname) []:docker-registry.com
15. Email Address []:
16.
17. Please enter the following ‘extra‘ attributes
18. to be sent with your certificate request
19. A challenge password []:
20. An optional company name []:
( 5) 签署认证请求
1. $ openssl x509 -req -in dev-docker-registry.com.csr -CA devdockerCA.crt -CAkey devdockerCA.key -CAcreateserial -out
domain.crt -days 10000
2) 配置nginx使用证书
修改registry.conf配置文件, 取消如下三行的注释:
1. ssl on;
2. ssl_certificate /etc/nginx/conf.d/domain.crt;
3. ssl_certificate_key /etc/nginx/conf.d/domain.key;
3) 运行Registry
执行docker-compose up -d在后台运行Registry, 并使用curl验证结果。 这时使用localhost:5000端口仍然可以直接访问Registry, 但是如
果使用443端口通过nginx代理访问, 因为已经加了SSL认证, 所以使用http将返回“400 bad request”
1. $ curl http://localhost:5000/v2/
2. {}
3. $ curl http://localhost:443/v2/
4. <html>
5. <head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
6. <body bgcolor="white">
7. <center><h1>400 Bad Request</h1></center>
8. <center>The plain HTTP request was sent to HTTPS port</center>
9. <hr><center>nginx/1.9.9</center>
10. </body>
11. </html>
应该使用https协议:
1. $ curl https://localhost:443/v2/
2. curl: (60) Peer certificate cannot be authenticated with known CA certificates
3. More details here: http://curl.haxx.se/docs/sslcerts.html
4.
5. curl performs SSL certificate verification by default, using a "bundle"
6. of Certificate Authority (CA) public keys (CA certs). If the default
7. bundle file isn‘t adequate, you can specify an alternate file
8. using the --cacert option.
9. If this HTTPS server uses a certificate signed by a CA represented in
10. the bundle, the certificate verification probably failed due to a
11. problem with the certificate (it might be expired, or the name might
12. not match the domain name in the URL).
13. If you‘d like to turn off curl‘s verification of the certificate, use
14. the -k (or --insecure) option.
15.
由于是使用的未经任何认证机构认证的证书, 并且还没有在本地应用自己生成的证书。 所以此时会提示使用的是未经认证的证书, 可以使
用“-k"选项不进行验证。
1. $ curl -k https://localhost:443/v2/
2. <html>
3. <head><title>401 Authorization Required</title></head>
4. <body bgcolor="white">
5. <center><h1>401 Authorization Required</h1></center>
6. <hr><center>nginx/1.9.9</center>
7. </body>
8. </html>
客户端使用Registry
1. 添加证书
Centos 6/7 添加证书具体步骤如下:
1) 安装ca-certificates包
1. $ yum install ca-certificates
2) 使能动态CA配置功能
1. $ update-ca-trust force-enable
3) 将key拷贝到/etc/pki/ca-trust/source/anchors/
1. $ cp devdockerCA.crt /etc/pki/ca-trust/source/anchors/
4) 使新拷贝的证书生效
1. $ update-ca-trust extract
证书拷贝后, 需要重启docker以保证docker能使用新的证书。
1. $ service docker restart
2. Docker pull/push image测试
制作要push到registry的镜像
1. #查看本地已有镜像
2. $ docker images
3. REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
4. registry 2 cd57aad0bd45 3 days ago 224.5 MB
5. nginx 1.9 813e3731b203 3 weeks ago 133.9 MB
6. #为本地镜像打标签
7. $ docker tag registry:2 docker-registry.com/registry:2
8. $ docker tag nginx:1.9 docker-registry.com/nginx:1.9
9. $ docker images
10. REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
11. registry 2 cd57aad0bd45 3 days ago 224.5 MB
12. docker-registry.com/registry 2 cd57aad0bd45 3 days ago 224.5 MB
13. nginx 1.9 813e3731b203 3 weeks ago 133.9 MB
14. docker-registry.com/nginx 1.9 813e3731b203 3 weeks ago 133.9 MB
push测试
1. #不登陆直接push镜像到registry, 会提示失败
2. [[email protected] ~]# docker push docker-registry.com/registry
3. The push refers to a repository [docker-registry.com/registry] (len: 1)
4. cd57aad0bd45: Image push failed
5. cd57aad0bd45: Buffering to Disk
6. Please login prior to push:
7. Username:
8. Error response from daemon: no successful auth challenge for https://docker-registry.com/v2/ - errors: [basic auth
attempt to https://docker-registry.com/v2/ realm "registry.localhost" failed with status: 401 Unauthorized]
9. #登陆后, 再试
10. $docker login https://docker-registry.com
11. Username: docker
12. Password:
13. Email:
14. WARNING: login credentials saved in /root/.docker/config.json
15. Login Succeeded
16.
17. #可以push 镜像到registry
18. $ docker push docker-registry.com/registry
19. The push refers to a repository [docker-registry.com/registry] (len: 1)
20. cd57aad0bd45: Image already exists
21. b3c39a7768ea: Image successfully pushed
22. 4725a48b84d4: Image successfully pushed
23. 7b4078296418: Image successfully pushed
24. 7bd663e30ad0: Image successfully pushed
25. 28864e830e4d: Image successfully pushed
26. 7bd2d56d8449: Image successfully pushed
27. af88597ec24b: Image successfully pushed
28. b2ae0a712b39: Image successfully pushed
29. 02e5bca4149b: Image successfully pushed
30. 895b070402bd: Image successfully pushed
31. Digest: sha256:92835b3e54c05b90e416a309d37ca02669eb5e78e14a0f5ccf44b90d4c21ed4c
搜索镜像
1. curl https://docker:[email protected]/v2/_catalog
2. {"repositories":["registry"]}
3. curl https://docker:[email protected]/v2/nginx/tags/list
4. {"name":"registry","tags":["2"]}
pull测试
1. $ docker logout https://docker-registry.com
2. Remove login credentials for https://docker-registry.com
3. #不登陆registry直接pull镜像也会失败
4. $ docker pull docker-registry.com/registry:2
5. Pulling repository docker-registry.com/registry
6. Error: image registry:2 not found
7. #登陆后再测试
8. $ docker login https://docker-registry.com
9. Username: docker
10. Password:
11. Email:
12. WARNING: login credentials saved in /root/.docker/config.json
13. Login Succeeded
14. #登陆后可以pull
15. $ docker pull docker-registry.com/registry:2
16. 1.9: Pulling from dev-docker-registry.com/registry
17. 6d1ae97ee388: Already exists
18. 8b9a99209d5c: Already exists
19. 3244b9987276: Already exists
20. 50e5c9c52d5d: Already exists
21. 146400830f31: Already exists
22. b412cc1cde63: Already exists
23. 7fe375038652: Already exists
24. c43f11a030f9: Already exists
25. 152297b50994: Already exists
26. 01e808fa2993: Already exists
27. 813e3731b203: Already exists
28. Digest: sha256:af688d675460d336259d60824cd3992e3d820a90b4f31015ef49dc234a00adc3
29. Status: Downloaded newer image for docker-registry.com/registry:2
参考链接: Digitalocean: How To Set Up a Private Docker Registry on Ubuntu 14.04
来源: <http://www.jianshu.com/p/f2705a5da6a2>

以上是关于在CentOS 6上搭建私有的Docker Registry的主要内容,如果未能解决你的问题,请参考以下文章

centos下搭建docker私有仓库

CentOS7搭建Docker私有仓库----Docker

Docker私有仓库

centos7 安装docker私有仓库

Centos7.1 搭建docker私有仓库

CentOS 7 : Docker私有仓库搭建和使用