Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add multistage Dockerfile for reduced image size. #680

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -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"]
CMD ["node", "apps/web/server.js"]
1 change: 1 addition & 0 deletions apps/web/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ module.exports = {
images: {
domains: ["d2szwvl7yo497w.cloudfront.net", "appx-wsb-gcp.akamai.net.in"], // Add your domain here
},
output: "standalone",
};
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down