From e7377a877146458ae00aada3370ca1c0e4ae0233 Mon Sep 17 00:00:00 2001 From: toom1996 <23cm.cn@gmail.com> Date: Wed, 21 Oct 2026 00:03:06 +0800 Subject: [PATCH] =?UTF-8?q?build:=20=E6=96=B0=E5=A2=9E=20Dockerfile.prebui?= =?UTF-8?q?lt=EF=BC=8C.dockerignore=20=E4=BF=9D=E7=95=99=20dist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 配合后端 scripts/push-build.sh:本机 astro build 后服务器只打镜像层,不跑 npm ci + build - .dockerignore 之前忽略 dist,会导致预构建 COPY dist 失败 - 新增 .gitattributes 固定 Dockerfile / yml / conf 为 LF --- .dockerignore | 2 +- .gitattributes | 8 ++++++++ Dockerfile.prebuilt | 23 +++++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 .gitattributes create mode 100644 Dockerfile.prebuilt diff --git a/.dockerignore b/.dockerignore index 90afc23..014a4ff 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,5 +1,5 @@ node_modules -dist +# ⚠️ 不要忽略 dist:本地预构建时 Dockerfile.prebuilt 需要 COPY 它 .git .github .gitea diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..c7e3fb1 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,8 @@ +# Dockerfile 必须 LF:CRLF 会让续行符 `\` 后面多出一个 \r,被当成指令参数 +Dockerfile text eol=lf +Dockerfile.* text eol=lf + +# 脚本与配置统一 LF +*.sh text eol=lf +*.yml text eol=lf +*.conf text eol=lf diff --git a/Dockerfile.prebuilt b/Dockerfile.prebuilt new file mode 100644 index 0000000..0808859 --- /dev/null +++ b/Dockerfile.prebuilt @@ -0,0 +1,23 @@ +# 预构建镜像:dist/ 在本机 build 好(构建期 SSG 数据经 SSH 隧道取自线上), +# 服务器只装运行时依赖并打层,不跑 astro build。 +# 由 backend/scripts/push-build.sh 与 backend/deploy.sh --prebuilt 使用。 + +FROM node:22-alpine +WORKDIR /app + +ENV NODE_ENV=production \ + HOST=0.0.0.0 \ + PORT=3000 + +# 运行时依赖层:只在 package-lock 变化时重装,日常发布命中缓存 +COPY package*.json ./ +RUN npm ci --omit=dev && npm cache clean --force + +COPY dist ./dist + +EXPOSE 3000 + +HEALTHCHECK --interval=15s --timeout=3s --start-period=15s --retries=3 \ + CMD nc -z 127.0.0.1 3000 || exit 1 + +CMD ["node", "./dist/server/entry.mjs"]