From 69c13e1a792cda1997b800f9ec39781c9ad4376a Mon Sep 17 00:00:00 2001 From: geekyharsh05 Date: Tue, 8 Oct 2024 03:17:54 +0530 Subject: [PATCH 1/2] feat: add Dockerfile for production build and deployment. --- Dockerfile.prod | 54 +++++++++++++++++++++++++++++++++-------- apps/web/next.config.js | 1 + apps/web/package.json | 1 + docker-compose.yml | 4 ++- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/Dockerfile.prod b/Dockerfile.prod index 5c1e63de..98d5bf6f 100644 --- a/Dockerfile.prod +++ b/Dockerfile.prod @@ -1,18 +1,52 @@ -FROM node:20-alpine -ARG DATABASE_URL +FROM node:20-alpine AS base + +# Stage 1: Install dependencies only when needed +FROM base AS deps +RUN apk add --no-cache libc6-compat WORKDIR /usr/src/app +COPY package.json yarn.lock* ./ +COPY apps/web/package.json ./apps/web/ +COPY packages/ ./packages/ + +RUN yarn install --frozen-lockfile --ignore-scripts + +# Stage 2: Rebuild the source code only when needed +FROM base AS builder +WORKDIR /usr/src/app + +COPY --from=deps /usr/src/app/node_modules ./node_modules + COPY . . -RUN npm install -RUN cd packages/db && DATABASE_URL=$DATABASE_URL npx prisma generate && cd ../.. -## put DATABASE_URL in apps/web/.env -RUN echo DATABASE_URL=$DATABASE_URL >> apps/web/.env -RUN DATABASE_URL=$DATABASE_URL npm run build -## Remove .env file -RUN rm apps/web/.env +RUN yarn global add turbo@^2.1.1 + +RUN DATABASE_URL=$DATABASE_URL npx prisma generate --schema packages/db/prisma/schema.prisma + +RUN cd apps/web && yarn add sharp +RUN yarn turbo run build --filter=web... + +# Stage 3: Production image +FROM base AS runner +WORKDIR /usr/src/app + +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs + +RUN mkdir .next +RUN chown nextjs:nodejs .next + +USER nextjs + +COPY --from=builder --chown=nextjs:nodejs /usr/src/app/apps/web/.next/standalone ./ +COPY --from=builder --chown=nextjs:nodejs /usr/src/app/apps/web/.next/static ./apps/web/.next/static +COPY --from=builder --chown=nextjs:nodejs /usr/src/app/apps/web/public ./apps/web/public EXPOSE 3000 +ENV NODE_ENV production +# For Standalone build +ENV PORT 3000 +ENV HOSTNAME "0.0.0.0" -CMD ["npm", "run", "start"] \ No newline at end of file +CMD ["node", "apps/web/server.js"] \ No newline at end of file diff --git a/apps/web/next.config.js b/apps/web/next.config.js index 1d532ed8..2de41c9f 100644 --- a/apps/web/next.config.js +++ b/apps/web/next.config.js @@ -27,4 +27,5 @@ module.exports = { images: { domains: ["d2szwvl7yo497w.cloudfront.net", "appx-wsb-gcp.akamai.net.in"], // Add your domain here }, + output: "standalone", }; diff --git a/apps/web/package.json b/apps/web/package.json index 7f77f57e..e4ec22a3 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -18,6 +18,7 @@ "@radix-ui/react-slot": "^1.0.2", "@repo/db": "*", "@repo/ui": "*", + "@repo/store": "*", "class-variance-authority": "^0.7.0", "clsx": "^2.1.0", "date-fns": "^3.6.0", diff --git a/docker-compose.yml b/docker-compose.yml index 46a8024a..7b772c03 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,8 @@ services: app: - build: . + build: + context: . + dockerfile: Dockerfile container_name: daily-code-docker environment: - DATABASE_URL=postgresql://postgres:postgres@db:5432/postgres From 519703d5edb9e5746eedcc655b5b5ef88867c9d9 Mon Sep 17 00:00:00 2001 From: geekyharsh05 Date: Sat, 7 Dec 2024 05:05:17 +0530 Subject: [PATCH 2/2] fix: update Dockerfile to remove frozen lockfile and modify image domains to remote patterns --- Dockerfile.prod | 2 +- apps/web/next.config.js | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Dockerfile.prod b/Dockerfile.prod index 98d5bf6f..9b87b2f0 100644 --- a/Dockerfile.prod +++ b/Dockerfile.prod @@ -10,7 +10,7 @@ COPY package.json yarn.lock* ./ COPY apps/web/package.json ./apps/web/ COPY packages/ ./packages/ -RUN yarn install --frozen-lockfile --ignore-scripts +RUN yarn install --ignore-scripts # Stage 2: Rebuild the source code only when needed FROM base AS builder diff --git a/apps/web/next.config.js b/apps/web/next.config.js index 2de41c9f..ba22a609 100644 --- a/apps/web/next.config.js +++ b/apps/web/next.config.js @@ -25,7 +25,18 @@ module.exports = { transpilePackages: ["@repo/ui", "@repo/common", "@repo/recoil"], images: { - domains: ["d2szwvl7yo497w.cloudfront.net", "appx-wsb-gcp.akamai.net.in"], // Add your domain here + remotePatterns: [ + { + protocol: 'https', + hostname: 'd2szwvl7yo497w.cloudfront.net', + pathname: '**', + }, + { + protocol: 'https', + hostname: 'appx-wsb-gcp.akamai.net.in', + pathname: '**', + }, + ], }, output: "standalone", };