docker創建私人倉庫

其實在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的配置添加到了主配置文件中。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章