From 2af066038351ab0634c359a688fe712a539992b4 Mon Sep 17 00:00:00 2001 From: Jan T Date: Wed, 14 Feb 2024 17:18:30 +0200 Subject: Optimize docker image size (#91) * Move prisma to runtime dependencies * Optimize Dockerfile and build script * Fix: remove mention of generated next-env.d.ts in Dockerfile * Add missing reset.d.ts file to Dockerfile * Remove compression steps from Dockerfile and entrypoint script * Add an env file with mocked env vars added for Docker production builds * Use server actions to get runtime env vars * Refactor types and names * Rollback serverActions, use parsed Zod object for runtime env * Reintroduce featureFlags object to avoid passing secret envs to the frontend * Improve string to boolean coercion Co-authored-by: Sebastien Castiel * Run prettier autoformat * Fix type issue, rename function to match behaviour better --------- Co-authored-by: Lauri Vuorela Co-authored-by: Sebastien Castiel --- Dockerfile | 53 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 14 deletions(-) (limited to 'Dockerfile') diff --git a/Dockerfile b/Dockerfile index d31173b..3fefd3c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,22 +1,47 @@ -FROM node:21-slim as base +FROM node:21-alpine as base -EXPOSE 3000/tcp WORKDIR /usr/app -COPY ./ ./ +COPY ./package.json \ + ./package-lock.json \ + ./next.config.js \ + ./tsconfig.json \ + ./reset.d.ts \ + ./tailwind.config.js \ + ./postcss.config.js ./ +COPY ./scripts ./scripts +COPY ./prisma ./prisma +COPY ./src ./src -RUN apt update && \ - apt install openssl -y && \ - apt clean && \ - apt autoclean && \ - apt autoremove && \ +RUN apk add --no-cache openssl && \ npm ci --ignore-scripts && \ - npm install -g prisma && \ - prisma generate + npx prisma generate -# env vars needed for build not to fail -ARG POSTGRES_PRISMA_URL -ARG POSTGRES_URL_NON_POOLING +ENV NEXT_TELEMETRY_DISABLED=1 +COPY scripts/build.env .env RUN npm run build -ENTRYPOINT ["/usr/app/scripts/container-entrypoint.sh"] +RUN rm -r .next/cache + +FROM node:21-alpine as runtime-deps + +WORKDIR /usr/app +COPY --from=base /usr/app/package.json /usr/app/package-lock.json ./ +COPY --from=base /usr/app/prisma ./prisma + +RUN npm ci --omit=dev --omit=optional --ignore-scripts && \ + npx prisma generate + +FROM node:21-alpine as runner + +EXPOSE 3000/tcp +WORKDIR /usr/app + +COPY --from=base /usr/app/package.json /usr/app/package-lock.json ./ +COPY --from=runtime-deps /usr/app/node_modules ./node_modules +COPY ./public ./public +COPY ./scripts ./scripts +COPY --from=base /usr/app/prisma ./prisma +COPY --from=base /usr/app/.next ./.next + +ENTRYPOINT ["/bin/sh", "/usr/app/scripts/container-entrypoint.sh"] -- cgit v1.3