User Tools

Site Tools


wiki:dockerizing_next_js_monorepo_app

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

wiki/dockerizing_next_js_monorepo_app.txt · Last modified: 2025/02/14 13:11 by antisa

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki