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

chore: Improve Docker size #1456

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
50 changes: 39 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
FROM node:18
# We are using multi-stage builds to reduce container size
# and only ship what's actually required by the app to run.
# https://docs.docker.com/get-started/09_image_best/#multi-stage-builds

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
FROM node:18-slim AS base
# https://github.com/prisma/prisma/issues/16232
RUN apt-get update && apt-get install -y openssl

# build with
# docker build \
Expand All @@ -22,13 +25,18 @@ ARG KEYCLOAK_ISSUER
ARG NEXTAUTH_SECRET
ARG NEXTAUTH_URL

# Build app
FROM base AS deps
WORKDIR /src

COPY package.json yarn.lock ./
COPY app/package.json ./app/
RUN yarn install --frozen-lockfile
RUN yarn install --frozen-lockfile --silent
# Need to separately install Sentry CLI, see https://github.com/getsentry/sentry-javascript/issues/8511
RUN yarn add @sentry/cli -W

ENV NODE_ENV production
ENV NODE_OPTIONS=--max_old_space_size=2048
ENV NEXT_TELEMETRY_DISABLED 1
ENV NEXT_PUBLIC_COMMIT=$COMMIT
ENV NEXT_PUBLIC_BASE_VECTOR_TILE_URL=$VECTOR_TILE_URL
ENV NEXT_PUBLIC_MAPTILER_STYLE_KEY=$MAPTILER_STYLE_KEY
Expand All @@ -37,15 +45,35 @@ ENV KEYCLOAK_SECRET=$KEYCLOAK_SECRET
ENV KEYCLOAK_ISSUER=$KEYCLOAK_ISSUER
ENV NEXTAUTH_SECRET=$NEXTAUTH_SECRET
ENV NEXTAUTH_URL=$NEXTAUTH_URL
ENV PORT 3000
# https://nextjs.org/docs/messages/sharp-missing-in-production
ENV NEXT_SHARP_PATH "/node_modules/sharp"

COPY ./ ./
FROM base AS builder
WORKDIR /src
COPY --from=deps /src/node_modules ./node_modules
COPY --from=deps /src/app/node_modules ./app/node_modules
COPY . .

RUN yarn prisma generate
RUN yarn build

# Install only prod dependencies and start app
RUN yarn install --frozen-lockfile --production && yarn cache clean
CMD npm start
FROM base AS runner
WORKDIR /src

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

USER nextjs

COPY --from=builder /src/app/public ./app/public
COPY --from=builder --chown=nextjs:nodejs /src/app/.next/standalone ./
# Need to have access to the Prisma schema to run migrations
COPY --from=builder --chown=nextjs:nodejs /src/app/prisma/schema.prisma ./schema.prisma
# Due to some complex dependencies of Prisma, we need to copy the node_modules from the deps stage
COPY --from=deps --chown=nextjs:nodejs /src/node_modules ./node_modules
# COPY --from=deps --chown=nextjs:nodejs /src/app/node_modules ./app/node_modules

EXPOSE 3000

CMD npm run docker:start

EXPOSE 3000
Loading