Docker 镜像加速

配置 registry-mirrors 加快拉取,以及内网搭建私有仓库的做法。

Beginner
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.

镜像站不等于私有仓库

MirrorPrivate registry
PurposeCache public imagesStore your own images
Configregistry-mirrorsinsecure-registries / TLS certs
Push supportNoYes

搭一个私有仓库

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
Docker 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)说明生效

Friends & Partners