feat(docker): add Dockerfile and .dockerignore for app containerization
This commit is contained in:
63
Dockerfile
Normal file
63
Dockerfile
Normal file
@ -0,0 +1,63 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
|
||||
# ---- Base image with pnpm enabled ----
|
||||
FROM node:20-slim AS base
|
||||
WORKDIR /app
|
||||
ENV NEXT_TELEMETRY_DISABLED=1 \
|
||||
PNPM_HOME=/usr/local/share/pnpm
|
||||
ENV PATH=${PNPM_HOME}:${PATH}
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends ca-certificates openssl \
|
||||
&& rm -rf /var/lib/apt/lists/* \
|
||||
&& corepack enable
|
||||
|
||||
# ---- Install dependencies (cached layer) ----
|
||||
FROM base AS deps
|
||||
COPY package.json pnpm-lock.yaml ./
|
||||
RUN pnpm install --frozen-lockfile
|
||||
|
||||
# ---- Build the Next.js application ----
|
||||
FROM base AS builder
|
||||
ARG DATABASE_URL="postgresql://postgres:postgres@localhost:5432/postgres"
|
||||
ARG NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=""
|
||||
ENV DATABASE_URL=${DATABASE_URL} \
|
||||
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=${NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY}
|
||||
COPY --from=deps /app/node_modules ./node_modules
|
||||
COPY . .
|
||||
RUN mkdir -p public
|
||||
RUN pnpm run prisma:generate
|
||||
RUN pnpm run build
|
||||
|
||||
# ---- Prune devDependencies for the runtime image ----
|
||||
FROM deps AS prod-deps
|
||||
RUN pnpm prune --prod
|
||||
|
||||
# ---- Production runtime ----
|
||||
FROM node:20-slim AS runner
|
||||
ARG NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=""
|
||||
WORKDIR /app
|
||||
ENV NODE_ENV=production \
|
||||
NEXT_TELEMETRY_DISABLED=1 \
|
||||
PORT=3000 \
|
||||
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=${NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY}
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends ca-certificates openssl \
|
||||
&& rm -rf /var/lib/apt/lists/* \
|
||||
&& mkdir -p /app \
|
||||
&& chown node:node /app
|
||||
|
||||
# Copy only what is required to run `next start`
|
||||
COPY --from=prod-deps --chown=node:node /app/node_modules ./node_modules
|
||||
COPY --from=builder --chown=node:node /app/package.json ./package.json
|
||||
COPY --from=builder --chown=node:node /app/.next ./.next
|
||||
# Copy public assets only if they exist in the project
|
||||
COPY --from=builder --chown=node:node /app/public ./public
|
||||
COPY --from=builder --chown=node:node /app/next.config.js ./next.config.js
|
||||
|
||||
# Keep the filesystem owned by the non-root user
|
||||
USER node
|
||||
|
||||
EXPOSE 3000
|
||||
|
||||
# `next start` will read any variables from the environment or a mounted .env.local
|
||||
CMD ["node", "./node_modules/next/dist/bin/next", "start"]
|
||||
Reference in New Issue
Block a user