From 86236aeb040223aab74e4bd22ddb21fe5646b2b2 Mon Sep 17 00:00:00 2001 From: Julien Froidefond Date: Mon, 1 Dec 2025 08:14:04 +0100 Subject: [PATCH] feat: docker and compose --- .dockerignore | 16 +++++++++++ Dockerfile | 67 ++++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 20 ++++++++++++++ next.config.mjs | 1 + 4 files changed, 104 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..8af1773 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,16 @@ +node_modules +.next +.git +.gitignore +.env +.env.local +.env*.local +npm-debug.log* +yarn-debug.log* +yarn-error.log* +.DS_Store +*.pem +README.md +.vscode +.idea + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a67edd5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,67 @@ +FROM node:20-alpine AS base + +# Install OpenSSL and libc6-compat for Prisma +RUN apk add --no-cache openssl3 libc6-compat + +# Install pnpm +RUN corepack enable && corepack prepare pnpm@latest --activate + +# Install dependencies only when needed +FROM base AS deps +WORKDIR /app + +# Copy package files +COPY package.json pnpm-lock.yaml ./ +RUN pnpm install --frozen-lockfile + +# Rebuild the source code only when needed +FROM base AS builder +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules +COPY . . + +# Build args for environment variables needed during build +ARG NEXTAUTH_SECRET=dummy-secret-for-build-only +ARG NEXTAUTH_URL=http://localhost:3000 +ARG DATABASE_URL=file:./prisma/dev.db + +# Set environment variables for build +ENV NEXTAUTH_SECRET=${NEXTAUTH_SECRET} +ENV NEXTAUTH_URL=${NEXTAUTH_URL} +ENV DATABASE_URL=${DATABASE_URL} +ENV NODE_ENV=production + +# Generate Prisma Client +RUN pnpm prisma generate + +# Build Next.js +RUN pnpm build + +# Production image, copy all the files and run next +FROM base AS runner +WORKDIR /app + +ENV NODE_ENV=production +ENV DATABASE_URL=file:./prisma/dev.db +ENV NEXTAUTH_URL=http://localhost:3000 + +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs + +# Copy necessary files +COPY --from=builder /app/public ./public +COPY --from=builder /app/.next/standalone ./ +COPY --from=builder /app/.next/static ./.next/static + +# Copy prisma schema (will be mounted as volume in dev) +COPY --from=builder /app/prisma ./prisma + +USER nextjs + +EXPOSE 3000 + +ENV PORT=3000 +ENV HOSTNAME="0.0.0.0" + +CMD ["node", "server.js"] + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b97ca72 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,20 @@ +services: + app: + build: + context: . + dockerfile: Dockerfile + args: + - NEXTAUTH_SECRET=${NEXTAUTH_SECRET:-dummy-secret-for-build-only} + - NEXTAUTH_URL=${NEXTAUTH_URL:-http://localhost:4000} + - DATABASE_URL=${DATABASE_URL:-file:./prisma/dev.db} + ports: + - "4000:3000" + volumes: + - ./prisma:/app/prisma + environment: + - NEXTAUTH_SECRET=${NEXTAUTH_SECRET} + - NEXTAUTH_URL=${NEXTAUTH_URL:-http://localhost:4000} + - DATABASE_URL=${DATABASE_URL:-file:./prisma/dev.db} + env_file: + - .env + diff --git a/next.config.mjs b/next.config.mjs index bd34191..f0ec1c0 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -6,6 +6,7 @@ const nextConfig = { images: { unoptimized: true, }, + output: 'standalone', }; export default nextConfig;