{{tag>js nextjs docker}} ====== Dockerizing next.js monorepo app ====== Example docker file: FROM node:20-slim AS base ENV PNPM_HOME="/pnpm" ENV PATH="$PNPM_HOME:$PATH" RUN corepack enable # below is workaround for https://github.com/mendableai/firecrawl/issues/1126 RUN corepack prepare pnpm@9.15.0 --activate FROM base AS build COPY . /usr/src/app WORKDIR /usr/src/app RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile RUN pnpm turbo run build RUN pnpm --filter=app1 --prod deploy /prod/app1 RUN pnpm --filter=app1 --prod deploy /prod/app1 FROM base AS app1 COPY --from=build /prod/app1 /app/ COPY --from=build /usr/src/app/apps/app1/.next /app/.next WORKDIR /app EXPOSE 4000 CMD [ "pnpm", "start" ] FROM base AS app2 COPY --from=build /prod/app2 /app/ COPY --from=build /usr/src/app/apps/app2/.next /app/.next WORKDIR /app EXPOSE 5000 CMD [ "pnpm", "start" ] ''pnpm run build'' assumes that there is a script that triggers the next build ex. from package.json: ... "scripts": { "dev": "next dev --turbopack -p 4000", "build": "next build", "start": "next start -p 4000", ... To build the images for separate apps run: docker build . --target app1 --tag app1:latest docker build . --target app2 --tag app2:latest ====== Tested on ====== * ====== See also ====== ====== References ====== * https://pnpm.io/docker#example-2-build-multiple-docker-images-in-a-monorepo