Files
AIRouter/docker-mirror-setup.md
2025-11-10 23:15:36 +08:00

263 lines
5.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
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 镜像加速器配置指南
## 问题说明
错误信息显示无法连接到 Docker Hub (`registry-1.docker.io`),这是因为网络访问受限导致的。
## 解决方案
### 方案一:配置 Docker 镜像加速器(推荐)
#### 1. 创建或编辑 Docker 配置文件
```bash
# 创建 docker 配置目录(如果不存在)
sudo mkdir -p /etc/docker
# 编辑 daemon.json 配置文件
sudo nano /etc/docker/daemon.json
```
#### 2. 添加以下配置
```json
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com",
"https://docker.nju.edu.cn"
],
"insecure-registries": [],
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
```
#### 3. 重启 Docker 服务
```bash
# 重新加载配置
sudo systemctl daemon-reload
# 重启 Docker
sudo systemctl restart docker
# 验证配置
docker info | grep -A 5 "Registry Mirrors"
```
#### 4. 测试拉取镜像
```bash
# 测试拉取一个小镜像
docker pull alpine:latest
# 如果成功,继续构建项目
cd /home/AIRouter
docker compose up --build -d
```
### 方案二:使用国内替代基础镜像
如果镜像加速器仍然不稳定,可以修改 Dockerfile 使用国内镜像源。
#### 修改后端 Dockerfile
创建 `backend/Dockerfile.cn`
```dockerfile
# 使用国内 Go 镜像源
FROM golang:1.24-bookworm AS builder
WORKDIR /app
# 配置 Go 模块代理(使用国内镜像)
ENV GO111MODULE=on \
GOPROXY=https://goproxy.cn,https://goproxy.io,direct \
CGO_ENABLED=1
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -ldflags="-w -s" -o gateway main.go
# 运行阶段使用清华大学镜像源
FROM debian:bookworm-slim
RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list.d/debian.sources && \
apt-get update && \
apt-get install -y --no-install-recommends ca-certificates wget && \
rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY --from=builder /app/gateway .
RUN mkdir -p /app/data
ENV DB_PATH=/app/data/gateway.db
EXPOSE 8080
CMD ["./gateway"]
```
#### 修改前端 Dockerfile
创建 `frontend/Dockerfile.cn`
```dockerfile
FROM node:20-alpine AS builder
WORKDIR /app
# 配置 npm 使用国内镜像
RUN npm config set registry https://registry.npmmirror.com
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM nginx:1.27-alpine
COPY --from=builder /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
RUN echo "OK" > /usr/share/nginx/html/health
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
```
#### 修改 docker-compose.yml
```yaml
version: '3.8'
services:
backend:
build:
context: ./backend
dockerfile: Dockerfile.cn # 使用国内优化版本
# ... 其他配置保持不变
frontend:
build:
context: ./frontend
dockerfile: Dockerfile.cn # 使用国内优化版本
# ... 其他配置保持不变
```
### 方案三:手动拉取所需镜像
```bash
# 使用镜像加速器手动拉取所需的基础镜像
docker pull golang:1.24-bookworm
docker pull debian:bookworm-slim
docker pull node:20-alpine
docker pull nginx:1.27-alpine
# 然后再构建项目
docker compose up --build -d
```
### 方案四:使用代理(临时方案)
如果你有可用的代理服务器:
```bash
# 设置 Docker 客户端代理
export HTTP_PROXY="http://your-proxy:port"
export HTTPS_PROXY="http://your-proxy:port"
export NO_PROXY="localhost,127.0.0.1"
# 然后执行构建
docker compose up --build -d
# 构建完成后取消代理
unset HTTP_PROXY HTTPS_PROXY NO_PROXY
```
或者配置 Docker daemon 使用代理:
```bash
# 创建 systemd 配置目录
sudo mkdir -p /etc/systemd/system/docker.service.d
# 创建代理配置文件
sudo nano /etc/systemd/system/docker.service.d/http-proxy.conf
```
添加以下内容:
```ini
[Service]
Environment="HTTP_PROXY=http://your-proxy:port"
Environment="HTTPS_PROXY=http://your-proxy:port"
Environment="NO_PROXY=localhost,127.0.0.1"
```
然后重启 Docker
```bash
sudo systemctl daemon-reload
sudo systemctl restart docker
```
## 推荐操作步骤
1. **首先尝试方案一**(配置镜像加速器)- 最简单且长期有效
2. 如果方案一不稳定,**使用方案三**(手动拉取镜像)
3. 如果以上都不行,**使用方案二**(国内优化 Dockerfile
## 验证成功
配置完成后,执行以下命令验证:
```bash
# 查看 Docker 配置
docker info
# 测试拉取镜像速度
time docker pull alpine:latest
# 构建项目
cd /home/AIRouter
docker compose up --build -d
# 查看构建进度
docker compose logs -f
# 检查服务状态
docker compose ps
```
## 常见问题
### Q: 镜像加速器配置后仍然超时?
A: 尝试使用不同的镜像源,或者切换到方案二使用国内优化的 Dockerfile。
### Q: 某些镜像源不可用?
A: 镜像源可能会变化,可以尝试其他可用的源,或搜索最新的国内 Docker 镜像源。
### Q: 构建速度仍然很慢?
A: 考虑使用本地镜像缓存,或者在网络条件好的时候提前拉取所需镜像。
## 其他国内可用镜像源
- 阿里云https://your-id.mirror.aliyuncs.com (需要注册获取专属地址)
- 腾讯云https://mirror.ccs.tencentyun.com
- 南京大学https://docker.nju.edu.cn
- 中国科技大学https://docker.mirrors.ustc.edu.cn
- 网易https://hub-mirror.c.163.com
- 百度云https://mirror.baidubce.com