Dockerfile 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # syntax=docker/dockerfile:1
  2. # 构建阶段:使用 Bun(快速包管理和构建)
  3. FROM --platform=$BUILDPLATFORM oven/bun:debian AS build-base
  4. WORKDIR /app
  5. FROM build-base AS deps
  6. COPY package.json ./
  7. RUN bun install
  8. FROM deps AS build
  9. COPY . .
  10. # 接收构建参数
  11. ARG APP_VERSION=dev
  12. # 设置为环境变量供 Next.js 使用
  13. ENV NEXT_TELEMETRY_DISABLED=1
  14. ENV NEXT_PUBLIC_APP_VERSION=$APP_VERSION
  15. # 构建时需要的环境变量 (避免数据库初始化错误)
  16. # 这些是占位符,实际运行时会被真实值覆盖
  17. ENV DSN="postgres://placeholder:placeholder@localhost:5432/placeholder"
  18. ENV REDIS_URL="redis://localhost:6379"
  19. # 标记为 CI 环境,跳过 instrumentation.ts 中的数据库连接
  20. ENV CI=true
  21. RUN bun run build
  22. # 运行阶段:使用 Node.js(避免 Bun 流式响应内存泄漏 Issue #18488)
  23. FROM node:trixie-slim AS runner
  24. ENV NODE_ENV=production
  25. ENV PORT=3000
  26. ENV HOST=0.0.0.0
  27. ENV HOSTNAME=0.0.0.0
  28. WORKDIR /app
  29. # 安装 PostgreSQL 18 客户端工具(用于数据库备份/恢复功能)和 curl(用于健康检查)
  30. # node:trixie-slim 基于 Debian Trixie,默认只有 PostgreSQL 17.x
  31. # 使用 PostgreSQL 官方仓库安装 18.x 以匹配 docker-compose 中的 postgres:18
  32. RUN apt-get update && \
  33. apt-get install -y --no-install-recommends curl ca-certificates gnupg && \
  34. curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor -o /etc/apt/keyrings/pgdg.gpg && \
  35. echo "deb [signed-by=/etc/apt/keyrings/pgdg.gpg] https://apt.postgresql.org/pub/repos/apt trixie-pgdg main" > /etc/apt/sources.list.d/pgdg.list && \
  36. apt-get update && \
  37. apt-get install -y --no-install-recommends postgresql-client-18 && \
  38. apt-get purge -y --auto-remove gnupg && \
  39. rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
  40. COPY --from=build --chown=node:node /app/public ./public
  41. COPY --from=build --chown=node:node /app/drizzle ./drizzle
  42. COPY --from=build --chown=node:node /app/messages ./messages
  43. COPY --from=build --chown=node:node /app/.next/standalone ./
  44. # Server Actions live inside .next/server; copy it or Next.js cannot resolve action IDs.
  45. COPY --from=build --chown=node:node /app/.next/server ./.next/server
  46. COPY --from=build --chown=node:node /app/.next/static ./.next/static
  47. USER node
  48. EXPOSE 3000
  49. CMD ["node", "server.js"]