Turborepo, Docker and multiple Nextjs Apps.

Hi guys, i need some help, i have a monorepo (turborepo) of nextjs proyects, and they have a lot of common dependencies like react, next, etc. So i'm trying to dockerize my monorepo, and trying to 1 only install common dependencies once and 2 trying to reduce the final image size of each service, i already try it nextjs standalone and fail 😦 . Anyone know how to fix this?
2 Replies
andersgee
andersgee•17mo ago
This is a Dockerfile that I used for several nextjs apps within a single turborepo. The way to reduce image size is by using layers and having a "runner" that only copies the relevant things needed for actually running the app.
#container from pruned repo
FROM node:16-alpine as base
RUN apk update

FROM base AS pruner
ARG SCOPE
ENV SCOPE ${SCOPE}
WORKDIR /app
RUN yarn global add turbo
COPY . .
RUN turbo prune --scope=${SCOPE} --docker


FROM base AS installer
WORKDIR /app
COPY --from=pruner /app/out/json/ .
COPY --from=pruner /app/out/yarn.lock ./yarn.lock
RUN yarn install --frozen-lockfile


FROM base AS builder
ARG SCOPE
ENV SCOPE ${SCOPE}
WORKDIR /app
COPY --from=installer /app/ .
COPY --from=pruner /app/out/full/ .
ENV NODE_ENV=production
RUN yarn turbo run build --filter=${SCOPE} --include-dependencies --no-deps


FROM base AS runner
ARG SCOPE
ENV SCOPE ${SCOPE}
WORKDIR /app
COPY --from=builder /app/ .
ENV NODE_ENV=production
CMD yarn turbo run start --filter=${SCOPE}
#container from pruned repo
FROM node:16-alpine as base
RUN apk update

FROM base AS pruner
ARG SCOPE
ENV SCOPE ${SCOPE}
WORKDIR /app
RUN yarn global add turbo
COPY . .
RUN turbo prune --scope=${SCOPE} --docker


FROM base AS installer
WORKDIR /app
COPY --from=pruner /app/out/json/ .
COPY --from=pruner /app/out/yarn.lock ./yarn.lock
RUN yarn install --frozen-lockfile


FROM base AS builder
ARG SCOPE
ENV SCOPE ${SCOPE}
WORKDIR /app
COPY --from=installer /app/ .
COPY --from=pruner /app/out/full/ .
ENV NODE_ENV=production
RUN yarn turbo run build --filter=${SCOPE} --include-dependencies --no-deps


FROM base AS runner
ARG SCOPE
ENV SCOPE ${SCOPE}
WORKDIR /app
COPY --from=builder /app/ .
ENV NODE_ENV=production
CMD yarn turbo run start --filter=${SCOPE}
This particular Dockerfile worked for me 7 months ago. I have not touched turborepo since then unfortunately and Im pretty sure turborepo have changed since then but hopyfully this gets you going!
oljimenez
oljimenez•17mo ago
Thanks