dockernuxt.jsturborepo

How to dockerize a nuxt project inside Turborepo?


I slightly modified the example Dockerfile from the documentation:

FROM node:lts-alpine AS base
 
FROM base AS builder
RUN apk add --no-cache libc6-compat
RUN apk update
# Set working directory
WORKDIR /app
RUN npm i -g turbo
COPY . .
RUN turbo prune @rusinas/blogue-app --docker
 
# Add lockfile and package.json's of isolated subworkspace
FROM base 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/package-lock.json ./package-lock.json
RUN npm install
 
# Build the project
COPY --from=builder /app/out/full/ .
RUN npx turbo run build --filter=web...
 
FROM base AS runner
WORKDIR /app
 
# Don't run production as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nuxtjs
USER nuxtjs
 
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=nuxtjs:nodejs /app/apps/web/.next/standalone ./
COPY --from=installer --chown=nuxtjs:nodejs /app/apps/web/.next/static ./apps/web/.next/static
COPY --from=installer --chown=nuxtjs:nodejs /app/apps/web/public ./apps/web/public
 
CMD node apps/web/server.js

The structure of my monorepo is pretty straighforward:

apps
  app
    *nuxt-app*
    Dockerfile
packages
  ui

turbo.json

And I am getting this error when I am trying to build the container:

 > [client builder 6/6] RUN turbo prune @rusinas/blogue-app --docker:
0.476 Turbo error: could not resolve workspaces: We did not find a package manager specified in your root package.json. Please set the "packageManager" property in your root package.json (https://nodejs.org/api/packages.html#packagemanager) or run `npx @turbo/codemod add-package-manager` in the root of your monorepo.

Outside of the docker, turbo prune @rusinas/blogue-app --docker works fine, so it looks like I need to put all the monorepo inside of the Dockerfile somehow, because I don't really get how it will be able to find my ui package for example. But this sounds stupid. What am I doing wrong?


Solution

  • Most probably your Docker build command doesn't have the right context.

    So when you run apps/app/Dockerfile Docker is using only the files found in the apps/app. Thus, when Turbo is trying to prune, it will lack the required files to do so.


    PS. Don't forget to create the .dockerignore file to exclude unnecessary file, otherwise the build process will take a while:

    node_modules
    npm-debug.log
    # ...
    

    Read more: Deploying with Docker