diff --git a/Dockerfile b/Dockerfile index b53eb03..c326ae6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,33 @@ +# syntax=docker/dockerfile:1 + # ---- Stage 1: Base ---- FROM node:22-alpine AS base -RUN apk add --no-cache libc6-compat -RUN corepack enable && corepack prepare pnpm@10.28.0 --activate +ARG ALPINE_MIRROR="" +ARG NPM_REGISTRY="" + +RUN if [ -n "$ALPINE_MIRROR" ]; then \ + sed -i "s|dl-cdn.alpinelinux.org|$ALPINE_MIRROR|g" /etc/apk/repositories; \ + fi && \ + apk add --no-cache libc6-compat + +RUN npm_registry="$NPM_REGISTRY"; \ + while [ "${npm_registry%/}" != "$npm_registry" ]; do \ + npm_registry="${npm_registry%/}"; \ + done; \ + if [ -n "$npm_registry" ]; then \ + export COREPACK_NPM_REGISTRY="$npm_registry"; \ + fi && \ + corepack enable && \ + corepack prepare pnpm@10.28.0 --activate WORKDIR /app # ---- Stage 2: Dependencies ---- FROM base AS deps +ARG NPM_REGISTRY + # Native build tools for sharp, @napi-rs/canvas RUN apk add --no-cache python3 build-base g++ cairo-dev pango-dev jpeg-dev giflib-dev librsvg-dev @@ -16,7 +35,15 @@ COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./ COPY packages/ ./packages/ COPY scripts/ ./scripts/ -RUN pnpm install --frozen-lockfile +RUN --mount=type=cache,id=pnpm-store,target=/root/.local/share/pnpm/store \ + npm_registry="$NPM_REGISTRY"; \ + while [ "${npm_registry%/}" != "$npm_registry" ]; do \ + npm_registry="${npm_registry%/}"; \ + done; \ + if [ -n "$npm_registry" ]; then \ + pnpm config set registry "$npm_registry"; \ + fi && \ + pnpm install --frozen-lockfile # ---- Stage 3: Builder ---- FROM base AS builder @@ -54,13 +81,22 @@ RUN pnpm build # ---- Stage 4: Runner ---- FROM node:22-alpine AS runner +ARG ALPINE_MIRROR="" + WORKDIR /app ENV NODE_ENV=production ENV HOSTNAME=0.0.0.0 ENV PORT=3000 -RUN apk add --no-cache libc6-compat cairo pango jpeg giflib librsvg +RUN if [ -n "$ALPINE_MIRROR" ]; then \ + cp /etc/apk/repositories /tmp/apk.repositories; \ + sed -i "s|dl-cdn.alpinelinux.org|$ALPINE_MIRROR|g" /etc/apk/repositories; \ + fi && \ + apk add --no-cache libc6-compat cairo pango jpeg giflib librsvg && \ + if [ -n "$ALPINE_MIRROR" ]; then \ + mv /tmp/apk.repositories /etc/apk/repositories; \ + fi RUN addgroup --system --gid 1001 nodejs && \ adduser --system --uid 1001 nextjs diff --git a/README-zh.md b/README-zh.md index 64655e1..2c3380f 100644 --- a/README-zh.md +++ b/README-zh.md @@ -287,6 +287,39 @@ cp .env.example .env.local docker compose up --build ``` +#### 慢速网络 / 中国大陆构建加速 + +Docker 构建支持两个可选参数。两者默认均为空,因此上面的标准命令仍会使用 +Alpine 和 npm 的上游软件源。 + +- `ALPINE_MIRROR` 接收不带 `https://` 的 Alpine 镜像站主机名。 +- `NPM_REGISTRY` 接收完整的 npm registry URL。 + +这些构建参数仅用于公共镜像地址。请勿在其中嵌入用户名、密码或访问令牌,因为 +Docker 可能把构建参数记录到镜像元数据或构建证明中。 + +使用 Docker Compose: + +```bash +ALPINE_MIRROR=mirrors.tuna.tsinghua.edu.cn \ +NPM_REGISTRY=https://registry.npmmirror.com \ +docker compose up --build +``` + +直接构建镜像: + +```bash +docker build \ + --build-arg ALPINE_MIRROR=mirrors.tuna.tsinghua.edu.cn \ + --build-arg NPM_REGISTRY=https://registry.npmmirror.com \ + -t openmaic:local . +``` + +这些参数不会加速 Docker Hub 拉取,包括 Dockerfile frontend 和 +`node:22-alpine` 基础镜像。若这些步骤较慢,需要单独配置 Docker daemon 的 +registry mirror。同一个 BuildKit builder 会在常规缓存清理前跨构建复用 pnpm +store;缓存只用于提升性能,不是正确完成构建的必要条件。 + ### 可选:MinerU(增强文档解析) [MinerU](https://github.com/opendatalab/MinerU) 提供更强的表格、公式和 OCR 解析能力。你可以使用 [MinerU 官方 API](https://mineru.net/) 或[自行部署](https://opendatalab.github.io/MinerU/quick_start/docker_deployment/)。 diff --git a/README.md b/README.md index 5ba71f4..05ac6bb 100644 --- a/README.md +++ b/README.md @@ -287,6 +287,42 @@ cp .env.example .env.local docker compose up --build ``` +#### Slow-network / China build acceleration + +Docker builds support two optional build arguments. Both are empty by default, +so the standard command above keeps using the upstream Alpine and npm +registries. + +- `ALPINE_MIRROR` is an Alpine mirror hostname without `https://`. +- `NPM_REGISTRY` is a complete npm registry URL. + +Use public mirror endpoints only. Do not embed usernames, passwords, or access +tokens in these build arguments because Docker may record them in image metadata +or build provenance. + +With Docker Compose: + +```bash +ALPINE_MIRROR=mirrors.tuna.tsinghua.edu.cn \ +NPM_REGISTRY=https://registry.npmmirror.com \ +docker compose up --build +``` + +For a direct image build: + +```bash +docker build \ + --build-arg ALPINE_MIRROR=mirrors.tuna.tsinghua.edu.cn \ + --build-arg NPM_REGISTRY=https://registry.npmmirror.com \ + -t openmaic:local . +``` + +These arguments do not accelerate Docker Hub pulls, including the Dockerfile +frontend and the `node:22-alpine` base image. Configure a Docker daemon registry +mirror separately if those pulls are slow. The pnpm store cache is reused by the +same BuildKit builder across builds, subject to normal cache garbage collection; +the cache only improves performance and is not required for a correct build. + ### Server-backed persistence (PostgreSQL) The `server-persistence` profile runs exactly two containers: the OpenMAIC app diff --git a/docker-compose.yml b/docker-compose.yml index ee91361..2726712 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,6 +3,8 @@ services: build: context: . args: + - ALPINE_MIRROR=${ALPINE_MIRROR:-} + - NPM_REGISTRY=${NPM_REGISTRY:-} # NEXT_PUBLIC_* values are compiled into the browser bundle. Leave them # empty unless the corresponding client feature is explicitly enabled; # persistence and other build-time flags can be supplied on the command line.