docker倉庫實際上提供兩方面的功能,一個是鏡像管理,一個是認證。前者主要由docker-registry項目來實現,經過http服務來上傳下載;後者能夠經過docker-index(閉源)項目或者利用現成認證方案(如nginx)實現http請求管理。最主要仍是實現本身公司業務的鏡像管理。
兩臺機器都裝有Docker version 1.13.1 一、192.168.188.128 私有倉庫 二、192.168.188.136 docker開發環境
安裝前將iptables,selinux給關閉linux
~]# yum install docker //安裝docker必須有extras倉庫 ~]# vim /etc/docker/daemon.json //使用國內站點,下載快 { "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"] } ~]# systemctl start docker.service
目的:實現認證和反向代理。讓倉庫更加安全可靠。nginx
~]# yum install httpd-devel pcre pcre-devel zlib zlib-devel openssl openssl-devel nginx -y ~]# vim /etc/nginx/conf.d/docker.conf \#定義registry地址,這裏定義127的地址 upstream docker-registry { server 127.0.0.1:5000; } \#定義域名及location server { listen 80; server_name docker.faqrobot.net; #設置nginx容許的POST請求數據包大小:默認爲1M,設置爲0則表示不限制 client_max_body_size 0; #請求轉發的的必要請求頭信息(虛擬主機地址,遠程主機地址,訪問token,服務器ip和端口) proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Authorization ""; proxy_set_header Accept-Encoding ""; proxy_set_header X-Forwarded-By $server_addr:$server_port; proxy_set_header X-Forwarded-For $remote_addr; # 第一個location用於訪問docker-registry,須要進行認證 location / { auth_basic "Restricted"; auth_basic_user_file /etc/nginx/auth/htpasswd.txt; proxy_pass http://docker-registry; proxy_buffering off; } #第二個location用於訪問docker-registry的images,關閉認證 \# location /v2/search { \# auth_basic off; \# proxy_pass http://docker-registry; \# } } #建立認證用戶 ~]# mkdir /etc/nginx/auth/ ~]# htpasswd -c /etc/nginx/auth/htpasswd.txt xiaoniaoo New password: Re-type new password: Adding password for user xiaoniaoo ~]# nginx //啓動nginx
~]# yum install docker 修改配置項 ~]# vim /usr/lib/systemd/system/docker.service //這裏添加就是http的請求,而非https,若是此處不改,push將出錯,有如下兩種方式解決,一就是在啓動腳本里加 ExecStart= ... --insecure-registry docker.faqrobot.net \ 二就是在/etc/docker/daemon.json { "insecure-registries":["hostname:port"] } ~]# systemctl daemon ~]# systemctl restart docker.service ~]# vim /etc/hosts //添加dns解析 192.168.188.128 docker.faqrobot.net
安裝方式:既然docker將一切容器化,那麼registry也必然能夠容器化,此篇幅直接使用官方的registry容器。也可以使用rpm或源碼安裝。docker
~]# docker pull registry ~]# docker run -d -p 127.0.0.1:5000:5000 --restart=always -v /data/registry:/var/lib/registry -v /data/config.yml:/etc/docker/registry/config.yml registry // /data/registry:/var/lib/registry 鏡像的保存目錄 // /data/config.yml:/etc/docker/registry/config.yml registry的配置文件 // --restart=always 在容器退出時老是重啓容器,主要應用在生產環境 ~]# docker exec -it registry sh / # vi /etc/docker/registry/config.yml version: 0.1 log: fields: service: registry storage: cache: blobdescriptor: inmemory filesystem: rootdirectory: /var/lib/registry //鏡像存放目錄位置,可掛載NAS或分佈式存儲 http: addr: :5000 //監聽端口 headers: X-Content-Type-Options: [nosniff] health: storagedriver: enabled: true interval: 10s threshold: 3 ~
嘗試push鏡像和pull鏡像
一、 192.168.188.136shell
~]# docker pull docker.io/busybox:latest //拉取官方busybox鏡像,鏡像小,作演示 ~]# docker tag docker.io/busybox docker.faqrobot.net/busybox //打標籤,要否則推不上去 ~]# docker push docker.faqrobot.net/busybox //推鏡像至倉庫 The push refers to a repository [docker.faqrobot.net/busybox] 0314be9edf00: Preparing unauthorized: authentication required //提示須要認證,由於我在私有倉庫上作nginx用到了認證。
登錄nginx:json
~]# docker login docker.farobot.net Username: xiaoniaoo Password: Login Succeeded //賬號密碼輸出正確,提示登錄成功
再次push鏡像:vim
~]# docker push docker.faqrobot.net/busybox The push refers to a repository [docker.faqrobot.net/busybox] 0314be9edf00: Layer already exists latest: digest: sha256:186694df7e479d2b8bf075d9e1b1d7a884c6de60470006d572350573bfa6dcd2 size: 527 //推送成功
二、192.168.188.128
查看私有倉庫中的鏡像是否上傳上來了:
以前定義的私有鏡像的存放目錄爲/data/registrytomcat
[root@localhost ~]# tree -L 2 /data/registry/docker/registry/v2/repositories/ /data/registry/docker/registry/v2/repositories/ ├── busybox //私有倉庫的鏡像存放路徑上已經有了busybox的目錄,說明已經存放上來了。 │ ├── _layers │ ├── _manifests │ └── _uploads └── tomcat ├── _layers ├── _manifests └── _uploads
遠程查看私有倉庫中是否有鏡像標記:安全
~]# curl -XGET http://docker.faqrobot.net/v2/_catalog {"repositories":["busybox","tomcat"]} ~]# curl -XGET http://docker.faqrobot.net/v2/busybox/tags/list {"name":"busybox","tags":["latest","v1.0"]} ~]# curl -XGET http://registry.docker.net/v2/tomcat/tags/list {"name":"tomcat","tags":["latest","v1.1"]}
search registry很是不友好,因而下一篇給你們推薦一個WEBGUI的倉庫管理工具。很是好用,可認證,search,權限設定也很合理。服務器