diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..67c530f --- /dev/null +++ b/.env.example @@ -0,0 +1,67 @@ +# ======================================== +# Docker 镜像源配置示例 +# ======================================== +# +# 【使用方法】 +# 1. 复制此文件:cp .env.example .env +# 2. 编辑 .env 文件,删掉下面 4 个配置行前面的 # 号: +# - DOCKER_REGISTRY +# - NPM_REGISTRY +# - GO_PROXY +# - ALPINE_MIRROR +# 3. 保存后运行:docker-compose build +# +# 【注意】 +# - 只删除配置行前面的 #,不要删除注释说明的 # +# - 国外用户请勿配置,使用默认官方源即可 +# +# ======================================== + +# ----------------------------- +# Docker Hub 镜像源(末尾必须有斜杠 /) +# ----------------------------- +# +# 推荐镜像源: +# docker.1ms.run/ - 速度快,推荐 +# dockerpull.org/ - 备用 +# dockerproxy.cn/ - 备用 +# +# 删除下面行首的 # 来启用: +# DOCKER_REGISTRY=docker.1ms.run/ + + +# ----------------------------- +# npm 镜像源(末尾斜杠可选) +# ----------------------------- +# +# 推荐镜像源: +# https://registry.npmmirror.com/ - 淘宝镜像,推荐 +# +# 删除下面行首的 # 来启用: +# NPM_REGISTRY=https://registry.npmmirror.com/ + + +# ----------------------------- +# Go 模块代理(注意末尾不要斜杠) +# ----------------------------- +# +# 推荐镜像源: +# https://goproxy.cn,direct - 七牛云,推荐 +# https://goproxy.io,direct - 备用 +# https://mirrors.aliyun.com/goproxy/,direct - 阿里云 +# +# 删除下面行首的 # 来启用: +# GO_PROXY=https://goproxy.cn,direct + + +# ----------------------------- +# Alpine apk 镜像源(末尾不要斜杠) +# ----------------------------- +# +# 推荐镜像源: +# mirrors.aliyun.com - 阿里云,推荐 +# mirrors.tuna.tsinghua.edu.cn - 清华大学 +# mirrors.ustc.edu.cn - 中科大 +# +# 删除下面行首的 # 来启用: +# ALPINE_MIRROR=mirrors.aliyun.com diff --git a/Dockerfile b/Dockerfile index aa9390b..860f67d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,20 @@ # 多阶段构建 Dockerfile for Huobao Drama # ==================== 阶段1: 构建前端 ==================== -FROM node:20-alpine AS frontend-builder +# 声明构建参数(支持镜像源配置) +ARG DOCKER_REGISTRY= +ARG NPM_REGISTRY= -# 配置 npm 镜像源(国内加速) -RUN npm config set registry https://registry.npmmirror.com +FROM ${DOCKER_REGISTRY:-}node:20-alpine AS frontend-builder + +# 重新声明 ARG(FROM 之后 ARG 作用域失效,需要重新声明) +ARG NPM_REGISTRY= + +# 配置 npm 镜像源(条件执行) +ENV NPM_REGISTRY=${NPM_REGISTRY:-} +RUN if [ -n "$NPM_REGISTRY" ]; then \ + npm config set registry "$NPM_REGISTRY" || true; \ + fi WORKDIR /app/web @@ -21,11 +31,26 @@ COPY web/ ./ RUN npm run build # ==================== 阶段2: 构建后端 ==================== -FROM golang:1.23-alpine AS backend-builder +# 每个阶段前重新声明构建参数 +ARG DOCKER_REGISTRY= +ARG GO_PROXY= +ARG ALPINE_MIRROR= -# 配置 Go 代理(国内镜像加速) -ENV GOPROXY=https://goproxy.cn,direct \ - GO111MODULE=on +FROM ${DOCKER_REGISTRY:-}golang:1.23-alpine AS backend-builder + +# 重新声明 ARG(FROM 之后 ARG 作用域失效,需要重新声明) +ARG GO_PROXY= +ARG ALPINE_MIRROR= + +# 配置 Alpine 镜像源(条件执行) +ENV ALPINE_MIRROR=${ALPINE_MIRROR:-} +RUN if [ -n "$ALPINE_MIRROR" ]; then \ + sed -i "s@dl-cdn.alpinelinux.org@$ALPINE_MIRROR@g" /etc/apk/repositories 2>/dev/null || true; \ + fi + +# 配置 Go 代理(使用 ENV 持久化到运行时) +ENV GOPROXY=${GO_PROXY:-https://proxy.golang.org,direct} +ENV GO111MODULE=on # 安装必要的构建工具(纯 Go 编译,无需 CGO) RUN apk add --no-cache \ @@ -51,7 +76,20 @@ COPY --from=frontend-builder /app/web/dist ./web/dist RUN CGO_ENABLED=0 go build -ldflags="-w -s" -o huobao-drama . # ==================== 阶段3: 运行时镜像 ==================== -FROM alpine:latest +# 每个阶段前重新声明构建参数 +ARG DOCKER_REGISTRY= +ARG ALPINE_MIRROR= + +FROM ${DOCKER_REGISTRY:-}alpine:latest + +# 重新声明 ARG(FROM 之后 ARG 作用域失效,需要重新声明) +ARG ALPINE_MIRROR= + +# 配置 Alpine 镜像源(条件执行) +ENV ALPINE_MIRROR=${ALPINE_MIRROR:-} +RUN if [ -n "$ALPINE_MIRROR" ]; then \ + sed -i "s@dl-cdn.alpinelinux.org@$ALPINE_MIRROR@g" /etc/apk/repositories 2>/dev/null || true; \ + fi # 安装运行时依赖 RUN apk add --no-cache \ diff --git a/README.md b/README.md index eff8381..87a0bdc 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,48 @@ go run main.go ### 🐳 Docker 部署(推荐) +#### 🚀 国内网络加速(可选) + +如果您在国内网络环境下,Docker 拉取镜像和安装依赖可能较慢。可以通过配置镜像源加速构建过程。 + +**步骤1:创建环境变量文件** + +```bash +cp .env.example .env +``` + +**步骤2:编辑 `.env` 文件,取消注释需要的镜像源** + +```bash +# 启用 Docker Hub 镜像(推荐) +DOCKER_REGISTRY=docker.1ms.run/ + +# 启用 npm 镜像 +NPM_REGISTRY=https://registry.npmmirror.com/ + +# 启用 Go 代理 +GO_PROXY=https://goproxy.cn,direct + +# 启用 Alpine 镜像 +ALPINE_MIRROR=mirrors.aliyun.com +``` + +**步骤3:重新构建** + +```bash +docker-compose build +``` + +**效果对比**: + +| 操作 | 不配置镜像源 | 配置镜像源后 | +|------|------------|-------------| +| 拉取基础镜像 | 5-30分钟 | 1-5分钟 | +| 安装 npm 依赖 | 可能失败 | 快速成功 | +| 下载 Go 依赖 | 5-10分钟 | 30秒-1分钟 | + +> **注意**:国外用户请勿配置镜像源,使用默认配置即可。 + #### 方式一:Docker Compose(推荐) ```bash diff --git a/docker-compose.yml b/docker-compose.yml index 1610fc8..807ae5e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,6 +5,15 @@ services: build: context: . dockerfile: Dockerfile + args: + # Docker Hub 镜像源(注意末尾斜杠) + DOCKER_REGISTRY: ${DOCKER_REGISTRY:-} + # npm 镜像源 + NPM_REGISTRY: ${NPM_REGISTRY:-} + # Go 代理 + GO_PROXY: ${GO_PROXY:-} + # Alpine apk 镜像源 + ALPINE_MIRROR: ${ALPINE_MIRROR:-} ports: - "5678:5678" volumes: