了解docker pull时下载的镜像

默认运行docker pull获取镜像时,其仓库地址为docker.io,执行docker info有:

% docker info
...
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
Registries: docker.io (secure)

完整镜像名称格式为<hostname>/<namespace|project>/<image>:<tag>

  • 镜像提供了完整主机名称,则docker从此镜像库下载镜像,如从quay.io镜像库的coreos项目下载etcd:latest镜像:
% docker pull quay.io/coreos/etcd
  • 若省略了hostname,则默认从docker.io下载镜像,而若省略project,其会补充projectlibrary
% docker pull library/debian
Using default tag: latest
Trying to pull repository docker.io/library/debian
% docker pull debian           # 未明确指定project,从docker.io下载时默认补充library/<image>
Using default tag: latest
Trying to pull repository docker.io/library/debian 

若没有提供hostname,则下载镜像顺序从Registries列表获取,但默认只有docker.io,可调整顺序,如:

% vi /etc/sysconfig/docker
ADD_REGISTRY='--add-registry okd-lr.zyl.io:5000 --add-registry quay.io'
INSECURE_REGISTRY='--insecure-registry okd-lr.zyl.io:5000' # 镜像库若是非TLS,则必须添加此参数

此时docker info显示如下:

Registry: https://okd-lr.zyl.io:5000/v1/     # 这里没啥用
Experimental: false
Insecure Registries:
 okd-lr.zyl.io:5000          # 非安全镜像库
 127.0.0.0/8
Live Restore Enabled: false  # 由ADD_REGISTRY添加了2个镜像库
Registries: okd-lr.zyl.io:5000 (insecure), quay.io (secure), docker.io (secure)

若此时下载镜像,则顺序遍历Registries列出的镜像库:

% docker pull etcd
Using default tag: latest
Trying to pull repository okd-lr.zyl.io:5000/etcd ...
Trying to pull repository quay.io/etcd ... 
Trying to pull repository docker.io/library/etcd ... 
Get https://registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io on 134.194.18.2:53: read udp 134.194.18.3:45004->134.194.18.2:53: i/o timeout

如上所示,docker顺序遍历了3个镜像库,而在前两个镜像库没找到后,从docker.io镜像库查找,此时其会添加library项目,而其他的镜像库地址不会添加。由于主机没法访问外网,则报错退出。

okd-lr.zyl.io:5000私有镜像库配置为docker.iomirror,如配置了REGISTRY_PROXY_REMOTEURL

% cat /etc/systemd/system/local-registry.service 
[Unit]
Description=Local Docker Mirror registry cache
Requires=docker.service
After=docker.service

# HTTP_PROXY、HTTPS_PROXY替换为实际代理
# NO_PROXY配置不走代理的IP地址,如示例192.168.10.x为宿主机IP地址
[Service]
ExecStartPre=-/usr/bin/docker rm -f local-registry
ExecStart=/usr/bin/docker run --name %p \
     -v /data/local-registry:/var/lib/registry \
     -e REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY=/var/lib/registry \
     -e REGISTRY_PROXY_REMOTEURL=https://registry.docker-cn.com \
     -e HTTP_PROXY=http://<ip>:<port>\
     -e HTTPS_PROXY=http://<ip>:<port>\
     -e NO_PROXY='localhost,127.0.0.1,.cluster.local,.svc,.zyl.io,172.30.0.1,192.168.10.3,192.168.10.4' \
     -p 5000:5000 registry
ExecStop=-/usr/bin/docker stop -t 2 %p
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target

此时配置–registry-mirror,如:

vi /etc/sysconfig/docker
OPTIONS='--registry-mirror=http://okd-lr.zyl.io:5000 --selinux-enabled --log-driver=journald --signature-verification=false'
ADD_REGISTRY='--add-registry okd-lr.zyl.io:5000 --add-registry quay.io'
INSECURE_REGISTRY='--insecure-registry okd-lr.zyl.io:5000'

此时执行docker pull debian下载镜像,有:

  1. 搜索okd-lr.zyl.io:5000/debian,若上面有则返回,否则搜索下一个镜像库;
  2. 搜索quay.io/debian,若成功则返回,否则继续搜索;
  3. 搜索docker.io/library/debian,因配置了registry-mirror,其将从此代理镜像库获取镜像,若代理镜像库中有,则直接返回,否则其先抓取镜像后再返回;

注意

  • registry-mirror只会代理docker.io默认镜像库的镜像,其他镜像库不会代理;
  • mirror镜像库支持的代理操作,即push/pull均会代理到docker.io上;
  • mirror镜像库若是非TLS配置,若要通过此mirror pushdocker.io,需配置INSECURE_REGISTRY
    原文作者:我是读书人
    原文地址: https://segmentfault.com/a/1190000019608267
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞