镜像加速

10.1 为什么需要加速

Docker Hub 官方镜像仓库在国外,国内访问慢甚至超时。需要配置国内镜像源加速拉取。

10.2 配置镜像加速器

daemon.json 配置方法

# 编辑配置文件
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<'EOF'
{
  "registry-mirrors": [
    "https://mirror.ccs.tencentyun.com",
    "https://docker.m.daocloud.io"
  ]
}
EOF
 
# 重启 Docker
sudo systemctl daemon-reload
sudo systemctl restart docker
 
# 验证
docker info | grep -A 5 "Registry Mirrors"

国内镜像源列表(2024 年可用)

提供商地址说明
腾讯云https://mirror.ccs.tencentyun.com推荐
DaoCloudhttps://docker.m.daocloud.io推荐
阿里云https://.mirror.aliyuncs.com需注册获取专属地址
华为云https://mirror.swr.myhuaweicloud.com需注册

阿里云专属镜像源

# 1. 登录阿里云容器镜像服务
# https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
 
# 2. 获取专属镜像加速地址(每个人不同)
# 格式:https://<your-id>.mirror.aliyuncs.com
 
# 3. 配置
sudo tee /etc/docker/daemon.json <<'EOF'
{
  "registry-mirrors": ["https://xxxxx.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

10.3 Docker Desktop 配置加速

macOS

Docker Desktop → Settings → Docker Engine → 添加:
{
  "registry-mirrors": [
    "https://mirror.ccs.tencentyun.com",
    "https://docker.m.daocloud.io"
  ]
}
→ Apply & Restart

Windows

Docker Desktop → Settings → Docker Engine → 同上

10.4 验证加速效果

# 测试拉取速度
time docker pull nginx:alpine
# 配置前:可能需要几分钟
# 配置后:几秒到几十秒
 
# 查看镜像源
docker info | grep -A 10 "Registry Mirrors"

10.5 构建加速

BuildKit 缓存挂载

# syntax=docker/dockerfile:1
# 依赖安装使用缓存,不随代码变化重建
RUN --mount=type=cache,target=/var/cache/apt \
    apt-get update && apt-get install -y python3
 
RUN --mount=type=cache,target=/root/.cache/pip \
    pip install -r requirements.txt
 
RUN --mount=type=cache,target=/app/node_modules \
    npm install
# 启用 BuildKit
export DOCKER_BUILDKIT=1
 
# 或在 daemon.json 中
{
  "features": {
    "buildkit": true
  }
}

多阶段并行构建

# BuildKit 自动并行构建无依赖的阶段
FROM node:20-alpine AS frontend
WORKDIR /app
COPY frontend/ .
RUN npm ci && npm run build
 
FROM golang:1.22 AS backend
WORKDIR /app
COPY backend/ .
RUN go build -o server .
 
# 两个阶段并行构建,最后合并
FROM alpine:3.19
COPY --from=frontend /app/dist /static
COPY --from=backend /app/server /server

10.6 代理配置

Docker Daemon 代理(拉取/推送镜像)

# 创建 systemd 配置目录
sudo mkdir -p /etc/systemd/system/docker.service.d
 
# 创建代理配置
sudo tee /etc/systemd/system/docker.service.d/http-proxy.conf <<'EOF'
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:8080"
Environment="HTTPS_PROXY=http://proxy.example.com:8080"
Environment="NO_PROXY=localhost,127.0.0.1,192.168.0.0/16"
EOF
 
# 重启
sudo systemctl daemon-reload
sudo systemctl restart docker

容器内代理(容器访问外网)

# 运行时设置
docker run -e http_proxy=http://proxy:8080 \
           -e https_proxy=http://proxy:8080 \
           alpine wget http://example.com
 
# 或在 Dockerfile 中
ENV http_proxy=http://proxy:8080
ENV https_proxy=http://proxy:8080

构建时代理

# BuildKit secret mount(不保存到镜像层)
RUN --mount=type=secret,id=proxy_env \
    cat /run/secrets/proxy_env | head -1
# 构建时传递代理
docker build --secret id=proxy_env,src=$HOME/.proxy .

10.7 镜像瘦身策略

选择小基础镜像

# 镜像大小对比
# ubuntu:22.04      ~77MB
# debian:bookworm    ~116MB
# python:3.12        ~1GB
# python:3.12-slim   ~150MB
# python:3.12-alpine ~50MB
# scratch            0MB(静态二进制)

多阶段构建瘦身

# 对比
# 构建阶段:golang:1.22 (~800MB)
# 运行阶段:scratch (~15MB)
# 节省:98%+

清理缓存

# Debian/Ubuntu
RUN apt-get update && apt-get install -y package && \
    apt-get clean && rm -rf /var/lib/apt/lists/*
 
# Alpine
RUN apk add --no-cache package
 
# Python
RUN pip install --no-cache-dir -r requirements.txt
 
# Node
RUN npm ci --only=production && npm cache clean --force

Docker Slim 工具

# 自动分析并瘦身镜像
docker run -it --privileged \
  -v /var/run/docker.sock:/var/run/docker.sock \
  ricoli/docker-slim build myapp:latest
 
# 结果:通常减少 50-90% 体积