docker配置国内镜像加速

docker配置国内镜像加速

前言

本篇只是最基础的配置国内镜像,不是什么奇法妙技

正文

使用docker测试拉取镜像时,出现了拉取失败的情况,如下:

1
2
3
4
[r@101 ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get "https://registry-1.docker.io/v2/": net/http: request canceled n (Client.Timeout exceeded while awaiting headers).
See 'docker run --help'.

这是由于访问官方镜像库超时了,接下来配置镜像加速

使用的是CentOS 7

  • 如果不存在 /etc/docker/daemon.json ,使用以下命令:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    # 创建目录
    sudo mkdir -p /etc/docker
    # 写入镜像配置
    sudo tee /etc/docker/daemon.json <<-'EOF'
    {
    "registry-mirrors": [
    "https://docker.m.daocloud.io",
    "https://dockerproxy.com",
    "https://docker.mirrors.ustc.edu.cn",
    "https://docker.nju.edu.cn"
    ]
    }
    EOF
    # 重启docker服务
    sudo systemctl daemon-reload
    sudo systemctl restart docker
    sudo systemctl status docker
    # 检查镜像源配置
    docker info | grep -A 5 "Registry Mirrors"
  • 如果已经存在 /etc/docker/daemon.json ,则

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    sudo cat > /etc/docker/daemon.json << EOF
    {
    "registry-mirrors": [
    "https://docker.m.daocloud.io",
    "https://dockerproxy.com",
    "https://docker.mirrors.ustc.edu.cn",
    "https://docker.nju.edu.cn"
    ]
    }
    EOF
    # 然后重启服务
    sudo systemctl daemon-reload
    sudo systemctl restart docker
    sudo systemctl status docker
    # 检查镜像源配置
    docker info | grep -A 5 "Registry Mirrors"

之后执行 docker run hello-world ,如果还是不行,检查网络问题或者换其他镜像源 换几次源才成功也不奇怪

结尾

又水了一篇ƪ(˘⌣˘)ʃ