Skip to content

Instantly share code, notes, and snippets.

@nickjanssen
Created February 8, 2025 00:26
Show Gist options
  • Save nickjanssen/a1d16f60c6764b83b1220f73fcfd3ec3 to your computer and use it in GitHub Desktop.
Save nickjanssen/a1d16f60c6764b83b1220f73fcfd3ec3 to your computer and use it in GitHub Desktop.
ARG BASE_IMAGE_TAG=latest
FROM registry.example.com/root/my-app/base:${BASE_IMAGE_TAG} AS builder
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk update
RUN apk add --no-cache libc6-compat
# Set working directory
WORKDIR /app
RUN pnpm add -g turbo
COPY . .
RUN turbo prune --scope=web --docker
# Add lockfile and package.json's of isolated subworkspace
FROM registry.example.com/root/my-app/base:${BASE_IMAGE_TAG} AS installer
RUN apk add --no-cache libc6-compat
RUN apk update
WORKDIR /app
# First install the dependencies (as they change less often)
COPY .gitignore .gitignore
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
RUN pnpm install
# Build the project
COPY --from=builder /app/out/full/ .
# Uncomment and use build args to enable remote caching
# ARG TURBO_TEAM
# ENV TURBO_TEAM=$TURBO_TEAM
ARG NEXT_PUBLIC_COMMIT_SHA
ENV NEXT_PUBLIC_COMMIT_SHA=$NEXT_PUBLIC_COMMIT_SHA
ARG NEXT_PUBLIC_ENV
ENV NEXT_PUBLIC_ENV=$NEXT_PUBLIC_ENV
ARG NEXT_PUBLIC_APP_URL
ENV NEXT_PUBLIC_APP_URL=$NEXT_PUBLIC_APP_URL
RUN pnpm turbo run build --filter=web...
RUN find . -name libquery_engine-linux-musl-openssl-3.0.x.so.node | head -n 1 | xargs -I {} cp {} /tmp/
FROM registry.example.com/root/my-app/base:${BASE_IMAGE_TAG} AS runner
WORKDIR /app
# Don't run production as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs
COPY --from=installer /app/apps/web/next.config.js .
COPY --from=installer /app/apps/web/package.json .
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/standalone ./
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/static ./apps/web/.next/static
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/public ./apps/web/public
RUN mkdir -p apps/web/.prisma/client/
COPY --from=installer --chown=nextjs:nodejs /tmp/libquery_engine-linux-musl-openssl-3.0.x.so.node ./apps/web/.prisma/client/
CMD node apps/web/server.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment