Merge pull request #32 from ShellMonster/feature/docker-mirror-source

feat: 添加Docker国内镜像源配置支持
This commit is contained in:
june-zj
2026-01-21 15:43:15 +08:00
committed by GitHub
3 changed files with 122 additions and 8 deletions

67
.env.example Normal file
View File

@@ -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

View File

@@ -1,10 +1,20 @@
# 多阶段构建 Dockerfile for Huobao Drama # 多阶段构建 Dockerfile for Huobao Drama
# ==================== 阶段1: 构建前端 ==================== # ==================== 阶段1: 构建前端 ====================
FROM node:20-alpine AS frontend-builder # 声明构建参数(支持镜像源配置)
ARG DOCKER_REGISTRY=
ARG NPM_REGISTRY=
# 配置 npm 镜像源(国内加速) FROM ${DOCKER_REGISTRY:-}node:20-alpine AS frontend-builder
RUN npm config set registry https://registry.npmmirror.com
# 重新声明 ARGFROM 之后 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 WORKDIR /app/web
@@ -21,11 +31,26 @@ COPY web/ ./
RUN npm run build RUN npm run build
# ==================== 阶段2: 构建后端 ==================== # ==================== 阶段2: 构建后端 ====================
FROM golang:1.23-alpine AS backend-builder # 每个阶段前重新声明构建参数
ARG DOCKER_REGISTRY=
ARG GO_PROXY=
ARG ALPINE_MIRROR=
# 配置 Go 代理(国内镜像加速) FROM ${DOCKER_REGISTRY:-}golang:1.23-alpine AS backend-builder
ENV GOPROXY=https://goproxy.cn,direct \
GO111MODULE=on # 重新声明 ARGFROM 之后 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 # 安装必要的构建工具(纯 Go 编译,无需 CGO
RUN apk add --no-cache \ 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 . RUN CGO_ENABLED=0 go build -ldflags="-w -s" -o huobao-drama .
# ==================== 阶段3: 运行时镜像 ==================== # ==================== 阶段3: 运行时镜像 ====================
FROM alpine:latest # 每个阶段前重新声明构建参数
ARG DOCKER_REGISTRY=
ARG ALPINE_MIRROR=
FROM ${DOCKER_REGISTRY:-}alpine:latest
# 重新声明 ARGFROM 之后 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 \ RUN apk add --no-cache \

View File

@@ -5,6 +5,15 @@ services:
build: build:
context: . context: .
dockerfile: Dockerfile 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: ports:
- "5678:5678" - "5678:5678"
volumes: volumes: