Next.js Discord

Discord Forum

Docker setup for nextjs, pnpm, prisma and turborepo

Unanswered
Dwarf Crocodile posted this in #help-forum
Open in Discord
Dwarf CrocodileOP
I have a setup using turborepo and running a few apps and some shared packages together and it works well in dev but i can't figure out how to setup docker, pnpm and prisma together. I've written my dockerfile below and my tree structure is

Prisma;
/apps/qrtag/prisma/prisma.schema

/apps/qrtag/(nextjs app)

/apps/qrtag/Dockerfile

/docker-compose.yml

Dockkerfile
# Builder
FROM node:alpine AS builder
RUN apk add --no-cache libc6-compat
RUN apk update

WORKDIR /app
RUN yarn global add turbo
COPY . .
COPY /apps/qrtag/prisma ./
RUN turbo prune --scope=qrtag --docker

# Installer
FROM node:alpine AS installer
RUN apk add --no-cache libc6-compat
RUN apk update

WORKDIR /app
RUN yarn global add pnpm
RUN yarn global add turbo

COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
COPY --from=builder /app/out/pnpm-workspace.yaml ./pnpm-workspace.yaml
RUN pnpm fetch
RUN pnpm install --offline
RUN pnpx prisma generate

COPY --from=builder /app/out/full/ .
COPY turbo.json turbo.json

# Uncomment and use build args to enable remote caching
# ARG TURBO_TEAM
# ENV TURBO_TEAM=$TURBO_TEAM

# ARG TURBO_TOKEN
# ENV TURBO_TOKEN=$TURBO_TOKEN

RUN turbo build --filter=qrtag...

# Runner
FROM node:alpine AS runner
WORKDIR /app

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs

COPY --from=installer /app/apps/qrtag/next.config.js .
COPY --from=installer /app/apps/qrtag/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/qrtag/.next/standalone ./
COPY --from=installer --chown=nextjs:nodejs /app/apps/qrtag/.next/static ./apps/qrtag/.next/static
# COPY --from=installer --chown=nextjs:nodejs /app/apps/appname/public ./apps/appname/public

CMD node apps/qrtag/server.js


docker-compose.yml
version: '3.8'
services:
  mysql:
    image: mysql:5.7
    restart: always
    ports:
      - 3306:3306
    expose:
      - 3306
    environment:
      MYSQL_ROOT_PASSWORD: prisma
      MYSQL_PASSWORD: prisma
      MYSQL_USER: prisma
      MYSQL_DATABASE: qrtag
    volumes:
      - mysql:/var/lib/mysql
  app:
    container_name: qrtag
    build:
      context: .
      dockerfile: ./apps/qrtag/Dockerfile
    ports:
      - '3001:3001'
volumes:
  mysql: ~


.dockerignore
**/node_modules
node_modules

33 Replies

Dwarf CrocodileOP
bump
Dwarf CrocodileOP
bump
Alligator mississippiensis
what are you unable to figure out? what're the issues that you're having? what have you tried out? for starters, what happens when you run docker-compose?
Dwarf CrocodileOP
My problem is that the line RUN pnpx prisma generate i've also tried RUN npx prisma generate doesn't apply the codegen so the typescript buildstep fails.
Dwarf CrocodileOP
Also noteworthy that ignoring build errors doesnt work as prisma needs the files to function its not just the types
@Dwarf Crocodile Also noteworthy that ignoring build errors doesnt work as prisma needs the files to function its not just the types
try adding this to copy the prisma schema
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
COPY --from=builder /app/out/pnpm-workspace.yaml ./pnpm-workspace.yaml
COPY --from=builder /app/out/prisma ./
RUN pnpm fetch
RUN pnpm install --offline
RUN pnpx prisma generate
Dwarf CrocodileOP
It can't find the prisma folder is there a way to print the tree structure of the docker container so i can see where it is located?
Dwarf CrocodileOP
Yes but the container cant run because the build fails is there a way around this?
@Ray try to start it
Dwarf CrocodileOP
It just doesnt exist
@Ray or try this docker COPY --from=builder /prisma ./
Dwarf CrocodileOP
still not found
@Dwarf Crocodile still not found
COPY --from=builder /app/prisma ./
Dwarf CrocodileOP
nop, is it possible to turn on a verbose mode in docker-compose so that you can do an ls?
@Dwarf Crocodile nop, is it possible to turn on a verbose mode in docker-compose so that you can do an ls?
I think you could try to mount a volume to it, so you can see the folder
Dwarf CrocodileOP
Would i mount from the host or from the container?
@Dwarf Crocodile Would i mount from the host or from the container?
WORKDIR /app
RUN yarn global add turbo
COPY . .
COPY /apps/qrtag/prisma .
RUN turbo prune --scope=qrtag --docker

....
COPY --from=builder /app/prisma .

does this work?
Dwarf CrocodileOP
Nope the folder still isn't found
Would it be worth to copy the local node_modules into the container to mitigate the build step or is that really bad practice?
@Dwarf Crocodile Would it be worth to copy the local node_modules into the container to mitigate the build step or is that really bad practice?
well you could do that if you are building the image on your machine everytime
Dwarf CrocodileOP
No i just remembered that that wouldnt work since it's built in a gitlab ci after each commit
yeah so that doesn't work out
Dwarf CrocodileOP
I found BUILDKIT_PROGRESS=plain docker-compose build which makes ls work so i'll crawl the workspace and return if i find something
@Dwarf Crocodile I found `BUILDKIT_PROGRESS=plain docker-compose build` which makes ls work so i'll crawl the workspace and return if i find something
WORKDIR /app
RUN yarn global add turbo
COPY . .
RUN turbo prune --scope=qrtag --docker

....
COPY /apps/qrtag/prisma .
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
COPY --from=builder /app/out/pnpm-workspace.yaml ./pnpm-workspace.yaml
RUN pnpm fetch
RUN pnpm install --offline
RUN pnpx prisma generate

how about this?
Dwarf CrocodileOP
I get this error but when i add --workspace-root to the prisma generate line i get this error. I've tried to change to npx prisma generate but i still get the same error as it runs pnpm add under the hood
I think you need to do this
https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/generating-prisma-client#using-a-custom-output-path
since prisma generate will try to add @prisma/client to package.json
Dwarf CrocodileOP
I changed to a custom output path but the command still tries to add prisma as a dependency
7.616 Prisma schema loaded from schema.prisma
8.184  ERR_PNPM_ADDING_TO_ROOT  Running this command will add the dependency to the workspace root, which might not be what you want - if you really meant it, make it explicit by running this command again with the -w flag (or --workspace-root). If you don't want to see this warning anymore, you may set the ignore-workspace-root-check setting to true.
8.254 Error: Command failed with exit code 1: pnpm add prisma@5.12.1 -D
Dwarf CrocodileOP
I added a line so that the docker container adds the prisma client in the root on runtime, it isn't by any means perfect or good but it works for my current use case but now i get this error with the turbo build

Type error: Declaration emit for this file requires using private name 'i'. An explicit type annotation may unblock declaration emit.

these are the relevant lines

RUN pnpm install --offline
RUN pnpm add prisma@5.12.1 --workspace-root
RUN pnpm add @prisma/client@5.12.1 --workspace-root
RUN pnpx prisma generate --schema=./schema.prisma

COPY --from=builder /app/out/full/ .
COPY turbo.json turbo.json

RUN turbo build --filter=qrtag...
Alligator mississippiensis
Have you checked this out? https://turbo.build/repo/docs/handbook/tools/prisma
It looks like doing this doesn't solve the issue with the client not being generated though
I thought that perhaps setting the generate script as a post install step would address it, but it doesn't. I think this issue would be a good issue to raise on turbo's GitHub