構建Docker私有倉庫可避免開發生產時可能產生的網絡問題;html
使用Docker Registry私有倉庫部署,使用Docker Auth作身份驗證nginx
考慮到使用場景:發佈鏡像通常須要認證,拉取鏡像則不須要,不一樣環境也須要不一樣的訪問策略。簡單的http驗證擴展能力受限,docker_auth提供了基於token的docker registry驗證明現方式,能夠更好的支持實際場景:git
本文通過做者親自驗證,若是讀者實踐時出錯,歡迎在評論區指出github
若是是第一次安裝,能夠略過此步驟docker
sudo apt-get remove docker docker-engine docker.io containerd runc
sudo apt-get purge docker-ce docker-ce-cli containerd.io
sudo rm -rf /var/lib/docker
複製代碼
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo usermod -aG docker
複製代碼
更多安裝方法參考Install Docker Engine on Ubuntujson
配置阿里雲鏡像ubuntu
sudo tee /etc/docker/daemon.json << eof { "registry-mirrors": ["https://jioksect.mirror.aliyuncs.com"] } eof
sudo systemctl daemon-reload
sudo systemctl restart docker
複製代碼
mkdir -p /opt/docker_auth/config /opt/docker_auth/log && touch /opt/docker_auth/config/auth_config.yml
echo ' server: addr: ":5001" certificate: "/root/cert.pem" key: "/root/cert.key" token: issuer: "Auth Service" expiration: 900 users: "root": password: "${passwd}" "": {} acl: - match: {account: "root"} actions: ["*"] - match: {account: ""} # 匿名用戶只能拉取鏡像 actions: ["pull"]' > /opt/docker_auth/config/auth_config.yml
複製代碼
${passwd}
生成方式
htpasswd -nB root
htpasswd -nB root
執行時要求輸入的密碼就是docker login
時輸入的root
用戶密碼部署容器vim
docker run -d \
--name=docker_auth \
-p ${port}:5001 \
--restart=always \
-v /opt/docker_auth/config:/config:ro \
-v /root/cert.pem:/root/cert.pem:ro \
-v /root/cert.key:/root/cert.key:ro \
-v /opt/docker_auth/log:/logs \
cesanta/docker_auth:1.6.0 --v=2 --alsologtostderr /config/auth_config.yml
複製代碼
docker login
命令時,會向Docker Auth發起驗證請求docker pull registry:2.7.0
mkdir -p /opt/docker_registry/config /opt/docker_registry/data && touch /opt/docker_registry/config/config.yml
複製代碼
echo 'version: 0.1 log: fields: service: registry storage: delete: enabled: true cache: blobdescriptor: inmemory filesystem: rootdirectory: /var/lib/registry auth: token: autoredirect: true realm: ${docker_auth_url}/auth service: Docker registry issuer: Auth Service rootcertbundle: /root/cert.pem http: addr: :5000 tls: certificate: /root/cert.pem key: /root/cert.key headers: X-Content-Type-Options: [nosniff] health: storagedriver: enabled: true interval: 10s threshold: 3' > /opt/docker_registry/config/config.yml
複製代碼
${docker_auth_url}即爲Docker Auth服務的公網地址,bash
docker login
時會出現簽名錯誤Docker Auth默認提供的是HTTPS服務,因此**${docker_auth_url}**應當使用HTTPS協議markdown
證書能夠從阿里雲免費申請
docker run -d \
-p ${port}:5000 \
--restart=always \
--name=registry \
-v /opt/docker_registry/config/:/etc/docker/registry/ \
-v /opt/docker_registry/data:/var/lib/registry \
-v /root/cert.pem:/root/cert.pem:ro \
-v /root/cert.key:/root/cert.key:ro \
registry:2.3
複製代碼
echo 'server { listen 443 ssl; server_name ${host_name}; #ssl證書文件位置(常見證書文件格式爲:crt/pem) ssl_certificate /etc/nginx/ssl/registry-cert.pem; #ssl證書key位置 ssl_certificate_key /etc/nginx/ssl/registry-cert.key; ssl_session_timeout 10m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_prefer_server_ciphers on; location / { proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-For $host; proxy_set_header X-Real-IP $remote_addr; # 可以使用frp暴露內網服務 proxy_pass https://${host_name}; } }' >> /opt/nginx/dockerRegistry.conf
複製代碼
使用Docker 容器部署Nginx服務便可
在Nginx服務HTTPS 服務使用的證書能夠是Docker Auth服務使用的同一套證書
補充:須要在nginx.conf
配置文件的http
模塊中添加client_max_body_size 0;不然在鏡像比較大時會出現Request Entity too large
錯誤
echo 'version: '3.7' services: auth: image: cesanta/docker_auth:1.6.0 volumes: - /opt/docker_auth/config:/config:ro - /opt/docker_auth/log:/logs - /opt/docker_auth/ssl/registry-cert.pem:/root/cert.pem:ro - /opt/docker_auth/ssl/registry-cert.key:/root/cert.key:ro container_name: docker_auth restart: always command: --v=2 --alsologtostderr /config/auth_config.yml ports: - ${auth_port}:5001 docker_registry: image: registry:2.3 container_name: registry depends_on: - auth ports: - ${registry_port}:5000 volumes: - /opt/docker_registry/config:/etc/docker/registry - /opt/docker_registry/data:/var/lib/registry - /opt/docker_auth/ssl/registry-cert.pem:/root/cert.pem:ro - /opt/docker_auth/ssl/registry-cert.key:/root/cert.key:ro restart: always' >> /opt/docker_registry/registry.yaml
cd /opt/docker_registry && docker-compose -f registry.yaml up -d
複製代碼
vim /etc/docker/daemon.json
在json結構中添加以下節點
{
"insecure-registries":
[ "${registry_hostname}:${port}"]
}
複製代碼
重啓Docker服務
systemctl daemon-reload
systemctl restart docker
複製代碼
docker login ${registry_hostname}:${port}
docker tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]
eg:docker tag myApp:v1 localhost:8080/myname/myApp:v1
docker push [OPTIONS] NAME[:TAG]
eg:docker push localhost:8080/myname/myApp:v1
docker login --username username
docker tag my-image username/my-repo
docker push username/my-repo
docker pull [OPTIONS] NAME[:TAG]