Dockerfile 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. WORKDIR /app
  28. # 安装 PostgreSQL 18 客户端工具(用于数据库备份/恢复功能)和 curl(用于健康检查)
  29. # node:trixie-slim 基于 Debian Trixie,默认只有 PostgreSQL 17.x
  30. # 使用 PostgreSQL 官方仓库安装 18.x 以匹配 docker-compose 中的 postgres:18
  31. RUN apt-get update && \
  32. apt-get install -y --no-install-recommends curl ca-certificates gnupg && \
  33. curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor -o /etc/apt/keyrings/pgdg.gpg && \
  34. 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 && \
  35. apt-get update && \
  36. apt-get install -y --no-install-recommends postgresql-client-18 && \
  37. apt-get purge -y --auto-remove gnupg && \
  38. rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
  39. COPY --from=build --chown=node:node /app/public ./public
  40. COPY --from=build --chown=node:node /app/drizzle ./drizzle
  41. COPY --from=build --chown=node:node /app/messages ./messages
  42. COPY --from=build --chown=node:node /app/.next/standalone ./
  43. # Server Actions live inside .next/server; copy it or Next.js cannot resolve action IDs.
  44. COPY --from=build --chown=node:node /app/.next/server ./.next/server
  45. COPY --from=build --chown=node:node /app/.next/static ./.next/static
  46. USER node
  47. EXPOSE 3000
  48. CMD ["node", "server.js"]