Sam
Sam

Reputation: 21

Building applications: Monorepo + Turborepo + Dockerfile + Next.js + pnpm

I am having trouble to deploy my applications inside my mono repo. The goal is to build a docker image for each app. The docker building process fails over the step where next is building the application. It can't resolve the paths inside the files.

I believe that Docker is using a different working directory compared to when I build it outside of docker or when I'm using the dev server.

When I'm reproducing the steps from the Dockerfile in terminal the build works.

That's a snippet of the build process where it fails:

#22 0.641 api:build:    ▲ Next.js 15.1.0
#22 0.641 api:build: 
#22 0.646 api:build:    Creating an optimized production build ...
#22 1.569 api:build: Failed to compile.
#22 1.569 api:build: 
#22 1.569 api:build: ./app/api/v1/browser/change-password/route.ts
#22 1.569 api:build: Module not found: Can't resolve '@/app/api/v1/browser/proxyFetch'
#22 1.569 api:build: 
#22 1.569 api:build: https://nextjs.org/docs/messages/module-not-found

How can I solve this?

tsconfig.json

{
  "extends": "@workspace/typescript-config/nextjs.json",
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./*"],
      "@workspace/ui/*": ["../../packages/ui/src/*"]
    },
    "plugins": [
      {
        "name": "next"
      }
    ]
  },
  "include": [
    "next-env.d.ts",
    "next.config",
    "**/*.ts",
    "**/*.tsx",
    ".next/types/**/*.ts"
  ],
  "exclude": ["node_modules"]
}

Dockerfile

FROM node:22-alpine AS base

WORKDIR /app

ENV NODE_ENV=production
ENV TURBO_TELEMETRY_DISABLED=1
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
ENV CI=1

RUN corepack enable pnpm

# =========================================================================== #
FROM base AS builder

RUN pnpm install --global turbo@^2

COPY . .

# https://turbo.build/repo/docs/guides/tools/docker#the-solution
RUN turbo prune api --docker
# =========================================================================== #

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
WORKDIR /app

RUN apk add --no-cache libc6-compat
RUN apk update

ARG TURBO_TEAM
ENV TURBO_TEAM=$TURBO_TEAM

ARG TURBO_TOKEN
ENV TURBO_TOKEN=$TURBO_TOKEN
RUN pnpm install --global turbo@^2

# First install the dependencies (as they change less often)
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/pnpm-lock.yaml ./pnpm-lock.yaml

RUN pnpm install --frozen-lockfile

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

# Build the project
RUN turbo run build --filter=api

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

ENV STRAPI_TOKEN=$STRAPI_TOKEN
ENV STRAPI_URL_DOCKER=$STRAPI_URL_DOCKER

CMD node apps/api/server.js

Upvotes: -1

Views: 48

Answers (0)

Related Questions