Skip to content

Commit 69c13e1

Browse files
committed
feat: add Dockerfile for production build and deployment.
1 parent 48ada6a commit 69c13e1

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed

Dockerfile.prod

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,52 @@
1-
FROM node:20-alpine
2-
ARG DATABASE_URL
1+
FROM node:20-alpine AS base
2+
3+
# Stage 1: Install dependencies only when needed
4+
FROM base AS deps
5+
RUN apk add --no-cache libc6-compat
36

47
WORKDIR /usr/src/app
58

9+
COPY package.json yarn.lock* ./
10+
COPY apps/web/package.json ./apps/web/
11+
COPY packages/ ./packages/
12+
13+
RUN yarn install --frozen-lockfile --ignore-scripts
14+
15+
# Stage 2: Rebuild the source code only when needed
16+
FROM base AS builder
17+
WORKDIR /usr/src/app
18+
19+
COPY --from=deps /usr/src/app/node_modules ./node_modules
20+
621
COPY . .
722

8-
RUN npm install
9-
RUN cd packages/db && DATABASE_URL=$DATABASE_URL npx prisma generate && cd ../..
10-
## put DATABASE_URL in apps/web/.env
11-
RUN echo DATABASE_URL=$DATABASE_URL >> apps/web/.env
12-
RUN DATABASE_URL=$DATABASE_URL npm run build
13-
## Remove .env file
14-
RUN rm apps/web/.env
23+
RUN yarn global add turbo@^2.1.1
24+
25+
RUN DATABASE_URL=$DATABASE_URL npx prisma generate --schema packages/db/prisma/schema.prisma
26+
27+
RUN cd apps/web && yarn add sharp
28+
RUN yarn turbo run build --filter=web...
29+
30+
# Stage 3: Production image
31+
FROM base AS runner
32+
WORKDIR /usr/src/app
33+
34+
RUN addgroup --system --gid 1001 nodejs
35+
RUN adduser --system --uid 1001 nextjs
36+
37+
RUN mkdir .next
38+
RUN chown nextjs:nodejs .next
39+
40+
USER nextjs
41+
42+
COPY --from=builder --chown=nextjs:nodejs /usr/src/app/apps/web/.next/standalone ./
43+
COPY --from=builder --chown=nextjs:nodejs /usr/src/app/apps/web/.next/static ./apps/web/.next/static
44+
COPY --from=builder --chown=nextjs:nodejs /usr/src/app/apps/web/public ./apps/web/public
1545

1646
EXPOSE 3000
47+
ENV NODE_ENV production
48+
# For Standalone build
49+
ENV PORT 3000
50+
ENV HOSTNAME "0.0.0.0"
1751

18-
CMD ["npm", "run", "start"]
52+
CMD ["node", "apps/web/server.js"]

apps/web/next.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ module.exports = {
2727
images: {
2828
domains: ["d2szwvl7yo497w.cloudfront.net", "appx-wsb-gcp.akamai.net.in"], // Add your domain here
2929
},
30+
output: "standalone",
3031
};

apps/web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"@radix-ui/react-slot": "^1.0.2",
1919
"@repo/db": "*",
2020
"@repo/ui": "*",
21+
"@repo/store": "*",
2122
"class-variance-authority": "^0.7.0",
2223
"clsx": "^2.1.0",
2324
"date-fns": "^3.6.0",

docker-compose.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
services:
22
app:
3-
build: .
3+
build:
4+
context: .
5+
dockerfile: Dockerfile
46
container_name: daily-code-docker
57
environment:
68
- DATABASE_URL=postgresql://postgres:postgres@db:5432/postgres

0 commit comments

Comments
 (0)