registry是docker提供的搭建自己私库的程序,但是一般用harbor做私库比较方便。
这里将registry配置为代理缓存模式,解决国内无法拉取官网镜像的问题。
registry 运行在香港服务器中。
# 拉取镜像 docker pull dqzboy/mirror-hub:latest # 打tag docker tag dqzboy/mirror-hub:latest mirror:latest # 指定挂载路径,可以将一块独立硬盘挂载到/mnt/docker提高可用空间。 docker run -dit --name hub --restart=always -v /mnt/docker:/var/lib/registry -p 5000:5000 mirror:latest 容器内会加载 /etc/distribution/config.yml 配置文件,里面定义了代理官方地址。 # auth_basic 认证,生成认证文件,root为账号。防止未授权访问。 htpasswd -c ./registry_auth root # nginx反向代理 server { listen 80; listen 443 ssl; http2 on; server_name hub.rootop.org; ssl_certificate /etc/letsencrypt/live/hub.rootop.org/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/hub.rootop.org/privkey.pem; ssl_session_timeout 5m; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305; ssl_prefer_server_ciphers on; add_header Strict-Transport-Security "max-age=604800; includeSubdomains; preload"; location / { auth_basic "Proxy Docker Hub"; auth_basic_user_file /home/software/openresty/nginx/conf/vhost/registry_auth; proxy_pass http://127.0.0.1:5000; proxy_set_header host $host; proxy_set_header Cookie $http_cookie; proxy_set_header scheme $scheme; } access_log /var/log/nginx/hub.rootop.org.log; error_log /var/log/nginx/hub.rootop.org_error.log; } # 登录,实际为nginx中认证,不会走到docker官方认证。 docker login hub.rootop.org 登录信息会保存在 /root/.docker/config.json 中,以base64编码保存账号密码。 # 指定域名拉取镜像 docker pull hub.rootop.org/library/nginx:latest # 本地docker设置镜像地址,免去前面输入域名。 [root@docker-server ~]# cat /etc/docker/daemon.json { "registry-mirrors": ["https://hub.rootop.org"] } [root@docker-server ~]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx b1badc6e5066: Pull complete a2da0c0f2353: Pull complete e5d9bb0b85cc: Pull complete 14a859b5ba24: Pull complete 716cdf61af59: Pull complete 14e422fd20a0: Pull complete c3741b707ce6: Pull complete Digest: sha256:33e0bbc7ca9ecf108140af6288c7c9d1ecc77548cbfd3952fd8466a75edefe57 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest # 补充 Docker Hub 把镜像组织成多个“命名空间”,例如: nginx/nginx:由 nginx 官方维护 myuser/myapp:自己上传的镜像 library/ubuntu:Docker 官方维护的基础镜像 而 library 是 Docker Hub 的默认命名空间,专门用于存放官方镜像。 Docker 客户端在执行 docker pull ubuntu时,会自动补全为 docker pull docker.io/library/ubuntu 只有在拉取 Docker Hub 的官方维护的镜像时才会自动加上 library。
原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/5615.html