Docker 镜像加速
配置 registry-mirrors 加快拉取,以及内网搭建私有仓库的做法。
Key Points加速靠改 daemon.json 的 registry-mirrors;只有 Docker Hub 的镜像走镜像站,其他仓库要单独配。
配置 daemon.json
JSON
{
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://dockerproxy.com"
],
"max-concurrent-downloads": 10,
"log-driver": "json-file",
"log-opts": { "max-size": "50m", "max-file": "3" }
}Bash
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json < /dev/null # 编辑上面内容
sudo systemctl daemon-reload
sudo systemctl restart docker
docker info | grep -A3 "Registry Mirrors"Docker Desktop 里配
Settings → Docker Engine, paste the JSON, Apply & Restart. Desktop uses the same daemon.json format.
镜像站不等于私有仓库
| Mirror | Private registry | |
|---|---|---|
| Purpose | Cache public images | Store your own images |
| Config | registry-mirrors | insecure-registries / TLS certs |
| Push support | No | Yes |
搭一个私有仓库
Bash
docker run -d -p 5000:5000 --name registry \
-v registry-data:/var/lib/registry \
registry:2
docker tag myapp:1.0 localhost:5000/myapp:1.0
docker push localhost:5000/myapp:1.0
docker pull localhost:5000/myapp:1.0Docker requires HTTPS for registries addressed by IP:port. Add the host to
insecure-registries for testing only — production needs a certificate.验证加速是否生效
Bash
docker pull nginx:alpine
# 输出里出现 mirror 域名(而不是 registry-1.docker.io)说明生效配置 daemon.json
JSON
{
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://dockerproxy.com"
],
"max-concurrent-downloads": 10,
"log-driver": "json-file",
"log-opts": { "max-size": "50m", "max-file": "3" }
}Bash
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json < /dev/null # 编辑上面内容
sudo systemctl daemon-reload
sudo systemctl restart docker
docker info | grep -A3 "Registry Mirrors"Docker Desktop 里配
Settings → Docker Engine,把 JSON 粘进去,Apply & Restart。Desktop 的配置文件和 Linux 的 daemon.json 是同一份格式。
镜像站不等于私有仓库
| 镜像加速 | 私有仓库 | |
|---|---|---|
| 用途 | 缓存公共镜像 | 存放自己的镜像 |
| 配置 | registry-mirrors | insecure-registries / TLS 证书 |
| 能否 push | 不能 | 能 |
搭一个私有仓库
Bash
docker run -d -p 5000:5000 --name registry \
-v registry-data:/var/lib/registry \
registry:2
docker tag myapp:1.0 localhost:5000/myapp:1.0
docker push localhost:5000/myapp:1.0
docker pull localhost:5000/myapp:1.0用 IP:端口(非 localhost)访问时,Docker 默认要求 HTTPS。测试环境可在
insecure-registries 里加该地址,生产必须配证书。验证加速是否生效
Bash
docker pull nginx:alpine
# 输出里出现 mirror 域名(而不是 registry-1.docker.io)说明生效