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 4 commits 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
46 changes: 36 additions & 10 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
FROM node:20-alpine
ARG DATABASE_URL
FROM node:20-alpine AS base

FROM base AS deps
RUN set -ex; \
apk update; \
apk add --no-cache \
libc6-compat \
openssl

WORKDIR /usr/src/app

COPY package.json yarn.lock* ./
COPY apps/web/package.json ./apps/web/
COPY packages/ ./packages/

RUN yarn install --ignore-scripts

FROM base AS builder
WORKDIR /usr/src/app

COPY --from=deps /usr/src/app/node_modules ./node_modules
COPY . .

RUN yarn
RUN cd packages/db && DATABASE_URL=$DATABASE_URL npx prisma@5.12.0 generate && cd ../..
## put DATABASE_URL in apps/web/.env
RUN echo DATABASE_URL=$DATABASE_URL >> apps/web/.env
RUN DATABASE_URL=$DATABASE_URL yarn build
## Remove .env file
RUN rm apps/web/.env
RUN yarn global add turbo@^2.1.1
RUN DATABASE_URL=$DATABASE_URL npx prisma@5.12.0 generate --schema packages/db/prisma/schema.prisma

RUN cd apps/web && yarn add sharp
RUN yarn turbo run build --filter=web...

FROM base AS runner
WORKDIR /usr/src/app

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

RUN mkdir .next && 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
ENV PORT 3000
ENV HOSTNAME "0.0.0.0"

CMD ["yarn", "start"]
CMD ["node", "apps/web/server.js"]
14 changes: 13 additions & 1 deletion apps/web/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +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",
};
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