在CentOS 6上搭建私人的Docker Registry

來源:互聯網
上載者:User

標籤:

在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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.