Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
50 lines (45 loc) · 3.04 KB
/
Copy pathDockerfile
File metadata and controls
50 lines (45 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# syntax=docker/dockerfile:1
# 默认走公共 npm 源: 仓库的 check:registry 不允许私有镜像地址进入已提交文件。要用局域网 NAS 的 Nexus 缓存,在 Coolify 里给应用加构建变量 NPM_REGISTRY(见 localcool 的 intranet-cache.md)。
#
# localcool 的静态站点 Dockerfile 模板(pnpm + Node 构建,nginx 提供)。复制到项目根目录并改名为 Dockerfile。
# 不使用 BuildKit 缓存卷,也不依赖清理策略: 下载成本由 NAS 上的 Nexus 代理缓存承担,所有项目和所有构建共用它。
# 层缓存仍然有效: 依赖层只在锁文件变化时重做。
FROM node:24-bookworm-slim AS build
# npm 源。用 IP 而不是 truenas.local: 构建容器里解析不了 mDNS 名字。
# npm 读 npm_config_registry,pnpm 读 ~/.npmrc。锁文件里默认源的包不记录下载地址,换源不需要改锁文件。
ARG NPM_REGISTRY=https://registry.npmjs.org/
ENV npm_config_registry=${NPM_REGISTRY}
# pnpm 12 不读 npm_config_registry 环境变量(只读 ~/.npmrc 的 registry=),否则 pnpm install 会静默走 registry.npmjs.org。
RUN printf 'registry=%s\n' "${NPM_REGISTRY}" > /root/.npmrc
ARG PNPM_VERSION=12.16.1
RUN npm install -g pnpm@${PNPM_VERSION}
WORKDIR /app
# 依赖层: 只复制清单文件,源码变化不会使它失效。
COPY package.json pnpm-lock.yaml ./
# pnpm 12 reads overrides / allowBuilds / minimumReleaseAgeExclude from pnpm-workspace.yaml, not package.json. Without it
# the install ignores them and --frozen-lockfile can fail. The star keeps the template working for a repo that has none.
COPY pnpm-workspace.yaml* ./
RUN pnpm install --frozen-lockfile
COPY . .
# 构建命令来自 package.json 的脚本名,默认 build; 项目需要别的(如 build:nd)时用 --build-arg 或 Coolify 的构建参数覆盖。
ARG BUILD_SCRIPT=build
# 构建开头会从 https://index.hagicode.com 拉取快照(about.json / tips-*.json),网络抖动时会以 "fetch failed" 失败。
# 快照步骤在构建最前面,失败很快,所以整轮重试的成本很低;真正的代码错误会重试同样次数后才失败。
ARG BUILD_ATTEMPTS=3
RUN i=1; while [ "$i" -le "$BUILD_ATTEMPTS" ]; do \
pnpm run "$BUILD_SCRIPT" && exit 0; \
echo "build attempt $i of $BUILD_ATTEMPTS failed" >&2; \
i=$((i + 1)); sleep 20; \
done; \
echo "build failed after $BUILD_ATTEMPTS attempts" >&2; exit 1
FROM nginx:alpine
# 输出目录默认 dist。
ARG DIST_DIR=dist
COPY --from=build /app/${DIST_DIR} /usr/share/nginx/html
# 目录重定向(/about -> /about/)默认写绝对地址,即 http://<容器主机名>/...;经 Coolify 的 HTTPS 代理访问时地址是错的。改为相对 Location。
# 不加 try_files: 默认配置对未知路径直接 404,不回退到 index.html(多页站点)。
# 有 404.html 的站点(docs)用它作为 404 页面;没有的保持 nginx 默认 404。
RUN printf 'absolute_redirect off;\n' > /etc/nginx/conf.d/00-relative-redirect.conf \
&& if [ -f /usr/share/nginx/html/404.html ]; then printf 'error_page 404 /404.html;\n' > /etc/nginx/conf.d/01-not-found.conf; fi \
&& nginx -t
EXPOSE 80