Files
docker_practice/03_install/3.9_mirror.md
2026-02-09 11:34:35 -08:00

118 lines
4.5 KiB
Go
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 镜像加速器
国内从 Docker Hub 拉取镜像有时会遇到困难此时可以配置镜像加速器
> **注意**镜像加速器的可用性经常变化配置前请先访问 [docker-practice/docker-registry-cn-mirror-test](https://github.com/docker-practice/docker-registry-cn-mirror-test/actions) 查看各镜像站的实时状态。
### 推荐配置方案
针对不同的使用场景我们推荐以下几种镜像加速配置方案以确保最佳的拉取速度
1. **云服务器用户**优先使用所在云平台提供的内部加速器见本页末尾
2. **本地开发用户**使用阿里云个人加速器或其他可用的公共加速器
3. **代理方案**如有条件可配置 HTTP 代理直接访问 Docker Hub
* [阿里云加速器](https://cr.console.aliyun.com/cn-hangzhou/instances)(需登录获取个人加速地址)
本节以 [AtomHub 可信镜像中心](https://hub.atomgit.com/) 镜像服务 `https://hub.atomgit.com` 为例进行介绍。
> `hub.atomgit.com` 仅包含部分官方镜像可以满足初学者的使用
### Ubuntu 16.04+Debian 8+CentOS 7+
目前主流 Linux 发行版均已使用 [systemd](https://systemd.io/) 进行服务管理,这里介绍如何在使用 systemd 的 Linux 发行版中配置镜像加速器。
请首先执行以下命令查看是否在 `docker.service` 文件中配置过镜像地址
```bash
$ systemctl cat docker | grep '\-\-registry\-mirror'
```
如果该命令有输出那么请执行 `$ systemctl cat docker` 查看 `ExecStart=` 出现的位置修改对应的文件内容去掉 `--registry-mirror` 参数及其值并按接下来的步骤进行配置
如果以上命令没有任何输出那么就可以在 `/etc/docker/daemon.json` 中写入如下内容如果文件不存在请新建该文件
```json
{
"registry-mirrors": [
"https://hub.atomgit.com"
]
}
```
> 注意一定要保证该文件符合 json 规范否则 Docker 将不能启动
之后重新启动服务
```bash
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
```
### Windows 10
对于使用 `Windows 10` 的用户在任务栏托盘 Docker 图标内右键菜单选择 `Change settings`打开配置窗口后在左侧导航菜单选择 `Docker Engine`在右侧像下边一样编辑 json 文件之后点击 `Apply & Restart` 保存后 Docker 就会重启并应用配置的镜像地址了
```json
{
"registry-mirrors": [
"https://hub.atomgit.com"
]
}
```
### macOS
对于使用 macOS 的用户在任务栏点击 Docker Desktop 应用图标 -> `Settings...`在左侧导航菜单选择 `Docker Engine`在右侧像下边一样编辑 json 文件修改完成之后点击 `Apply & restart` 按钮Docker 就会重启并应用配置的镜像地址了
```json
{
"registry-mirrors": [
"https://hub.atomgit.com"
]
}
```
### 检查加速器是否生效
执行 `$ docker info`如果从结果中看到了如下内容说明配置成功
```bash
Registry Mirrors:
https://hub.atomgit.com/
```
### `k8s.gcr.io` 镜像
可以登录 [阿里云 容器镜像服务](https://www.aliyun.com/product/acr?source=5176.11533457&userCode=8lx5zmtu&type=copy) **镜像中心**->**镜像搜索** 查找。
例如 `k8s.gcr.io/coredns:1.6.7` 镜像可以用 `registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.7` 代替
一般情况下有如下对应关系
```bash
## $ docker pull k8s.gcr.io/xxx
$ docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/xxx
```
### 不再提供服务的镜像
某些镜像不再提供服务添加无用的镜像加速器会拖慢镜像拉取速度你可以从镜像配置列表中删除它们
* https://dockerhub.azk8s.cn **已转为私有**
* https://reg-mirror.qiniu.com
* https://registry.docker-cn.com
* https://hub-mirror.c.163.com
* https://mirror.baidubce.com
建议 **watch页面右上角** [镜像测试](https://github.com/docker-practice/docker-registry-cn-mirror-test) 这个 GitHub 仓库,我们会在此更新各个镜像地址的状态。
### 云服务商
某些云服务商提供了 **仅供内部** 访问的镜像服务当您的 Docker 运行在云平台时可以选择它们
* [Azure 中国镜像 `https://dockerhub.azk8s.cn`](https://github.com/Azure/container-service-for-azure-china/blob/master/aks/README.md#22-container-registry-proxy)
* [腾讯云 `https://mirror.ccs.tencentyun.com`](https://cloud.tencent.com/act/cps/redirect?redirect=10058&cps_key=3a5255852d5db99dcd5da4c72f05df61)