perf(docker): add optional mirrors and pnpm cache (#1139)

* perf(docker): add optional mirrors and pnpm cache

* fix(docker): normalize optional npm registry
This commit is contained in:
Leyu 2026-08-23 18:30:25 +08:00 committed by GitHub
parent 5dc023d92d
commit 00880345e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 111 additions and 4 deletions

View File

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

View File

@ -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/)。

View File

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

View File

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