Selectively building docker images from apps #1013
-
Hi, I want to build a docker image per app for self-hosting but can't figure out how to split out the workspace packages. Imagine I have a pipeline that is supposed to build an image for each app, how can I make sure the image only contains the build of the app in question, e.g. just the invoices app? Or what is the suggested way to deploy these apps in a self-hosted scenario (sans Vercel)? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hey This article covers your problem Feel free to contribute to apps repository with your setup, we'd like to introduce a dockerfiles for apps for sure |
Beta Was this translation helpful? Give feedback.
-
I cobbled together a Dockerfile that builds each app, but one thing I am not sure of is environment variables. Also, I am a total turbo repo noob and maybe there is an easier way to start the service without having to supply the apps/[directory] - but from what I can see, that would require extending the turbo config. FROM node:18-alpine AS base
# name of workspace/app
ARG APP
# name of workspace/app folder
ARG APP_FOLDER
FROM base AS builder
ARG APP
ENV NEXT_TELEMETRY_DISABLED 1
RUN apk add --no-cache libc6-compat
RUN apk update
# Set working directory
WORKDIR /app
RUN npm i -g pnpm turbo
COPY . .
RUN turbo prune --scope=${APP} --docker
FROM base AS installer
ARG APP
ENV NEXT_TELEMETRY_DISABLED 1
RUN apk add --no-cache libc6-compat
RUN apk update
WORKDIR /app
RUN npm i -g pnpm turbo
COPY --from=builder /app/out/full .
RUN pnpm install
RUN yarn turbo run build --filter=${APP}
FROM base AS runner
ARG APP
ARG APP_FOLDER
WORKDIR /app
ENV NEXT_TELEMETRY_DISABLED 1
ENV START ${APP_FOLDER}
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs
COPY --from=installer --chown=nextjs:nodejs /app ./
WORKDIR /app/apps/${START}
CMD yarn run start
EXPOSE 3000
This builds an image like so:
As part of a pipeline in gitlab, the .env file needs to be created in the respective apps/[directory] before the build step or if someone wants to build them locally, that's a manual step to consider. I am not sure if nextjs picks up container env variables supplied via docker compose automatically? Last time I had to deal with that I was using cross-env, not sure if this works now. edit: Also, the resulting image is quite large at 800MB - I am sure there is room for improvement, but my goal is to get an app online and running ASAP. I was reading that you consider nextjs standalone mode in an issue here, that would certainly improve this situation. For now, I will be happy to have invoices running at all ;) |
Beta Was this translation helpful? Give feedback.
Hey
This article covers your problem
https://turbo.build/repo/docs/handbook/deploying-with-docker
Feel free to contribute to apps repository with your setup, we'd like to introduce a dockerfiles for apps for sure