Next.js Discord

Discord Forum

Docker Build Issue with Turborepo Remote Caching

Answered
Sander posted this in #help-forum
Open in Discord
## Problem
When using Turborepo remote caching in Docker multi-stage builds, the .next/static directory is missing during the COPY stage, causing the build to fail:

failed to solve: failed to compute cache key: failed to calculate checksum of ref 8584bfb4-5949-4507-8d0c-0c1bdbd34c24::vmqpro1ac6g4df92bekwijxvz: "/app/apps/codehouse/.next/static": not found


## Symptoms
- Build works fine when Turborepo cache is bypassed (invalid TURBO_TOKEN or no cache available)
- Build fails with "FULL TURBO" when using cached builds
- The .next directory doesn't exist when using cached builds
- All expected directories exist when cache is not used

## Debugging Attempts
Added debugging commands to verify filesystem state:

RUN ls -la /app
RUN ls -la /app/apps
RUN ls -la /app/apps/codehouse
>>>> RUN ls -la /app/apps/codehouse/.next # fails
RUN ls -la /app/apps/codehouse/.next/standalone
RUN ls -la /app/apps/codehouse/.next/static
RUN ls -la /app/apps/codehouse/public


## Configuration Changes Tried
1. Disabled caching in turbo.json:
   "build": {
     "cache": false
   },
   "codehouse#build": {
     "cache": false
   }
   

This works but defeats the purpose of remote caching.

2. Added outputFileTracingRoot to next.config.ts:
   outputFileTracingRoot: path.join(__dirname, '../../')
   

This should help with monorepo tracing but didn't resolve the issue.
Answered by Sander
So...you are telling me that
    "build": {
      "dependsOn": ["^build"],
      "env": [
        "NODE_ENV",
        "NEXT_PUBLIC_*",
        "VITE_*",
        "SENTRY_AUTH_TOKEN",
        "TURBO_TEAM",
        "TURBO_TOKEN",
        "APP_ENV",
        "APP_URL"
      ],
      "inputs": ["$TURBO_DEFAULT$", ".env*"],
      "outputs": [".next/**", "!.next/cache/**", "dist/**"]
    },
    "codehouse#build": {
      "dependsOn": ["@repo/runtime-env#build", "^build"]
    },

the "codehouse#build" is overriding the "build".
Oh that is just awful. I mean I understand it....I just didn't think of it
View full answer

4 Replies

## Dockerfile Content
FROM node:lts-alpine AS base

RUN npm install -g corepack@latest

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable pnpm

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

FROM base AS pruner
WORKDIR /app

COPY . .

RUN pnpm dlx turbo prune codehouse --docker

FROM base AS builder
WORKDIR /app

COPY --from=pruner /app/out/json/ .

RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm fetch --frozen-lockfile --prefer-frozen-lockfile
RUN pnpm install --offline

ARG CI=false
ENV CI=$CI

# Required for docker
ENV NEXT_OUTPUT=standalone

ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL

ARG NEXT_PUBLIC_ENV
ENV NEXT_PUBLIC_ENV=$NEXT_PUBLIC_ENV

ARG NEXT_PUBLIC_SENTRY_ENABLED=true
ENV NEXT_PUBLIC_SENTRY_ENABLED=$NEXT_PUBLIC_SENTRY_ENABLED

ARG TURBO_TEAM
ENV TURBO_TEAM=$TURBO_TEAM

COPY --from=pruner /app/out/pnpm-lock.yaml /app/out/full/ ./
RUN --mount=type=secret,id=SENTRY_AUTH_TOKEN --mount=type=secret,id=TURBO_TOKEN \
    export SENTRY_AUTH_TOKEN="$(cat /run/secrets/SENTRY_AUTH_TOKEN)" TURBO_TOKEN="$(cat /run/secrets/TURBO_TOKEN)" && \
    pnpm dlx turbo build

RUN ls -la /app
RUN ls -la /app/apps
RUN ls -la /app/apps/codehouse
RUN ls -la /app/apps/codehouse/.next
RUN ls -la /app/apps/codehouse/.next/standalone
RUN ls -la /app/apps/codehouse/.next/static
RUN ls -la /app/apps/codehouse/public

FROM base AS runner
WORKDIR /app

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

COPY --from=builder --chown=nextjs:nodejs /app/apps/codehouse/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/apps/codehouse/.next/static ./apps/codehouse/.next/static
COPY --from=builder --chown=nextjs:nodejs /app/apps/codehouse/public ./apps/codehouse/public

CMD ["node","apps/codehouse/server.js"]
## Current Status
The issue persists. When Turborepo uses cached builds.

## Environment
- Next.js 16.0.10 with standalone output
- Turborepo with remote caching enabled
- Docker multi-stage build
- Monorepo structure with packages in separate directory
As you can see here the .next folder does not exist on a turbo build
#23 [builder  9/13] RUN ls -la /app/apps/codehouse
#23 0.350 total 96
#23 0.350 drwxr-xr-x    1 root     root          4096 Jan 19 14:05 .
#23 0.350 drwxr-xr-x    1 root     root          4096 Jan 19 14:05 ..
#23 0.350 drwxr-xr-x    1 root     root          4096 Jan 19 14:05 .turbo
#23 0.350 -rw-r--r--    1 root     root           529 Jan 19 14:05 components.json
#23 0.350 -rw-r--r--    1 root     root           357 Jan 19 14:05 eslint.config.mjs
#23 0.350 -rw-r--r--    1 root     root           215 Jan 19 14:05 global.d.ts
#23 0.350 -rw-r--r--    1 root     root           108 Jan 19 14:05 i18n.config.ts
#23 0.350 drwxr-xr-x    2 root     root          4096 Jan 19 14:05 messages
#23 0.350 -rw-r--r--    1 root     root           247 Jan 19 14:05 next-env.d.ts
#23 0.350 -rw-r--r--    1 root     root          2219 Jan 19 14:05 next.config.ts
#23 0.350 drwxr-xr-x   13 root     root          4096 Jan 19 14:06 node_modules
#23 0.350 -rw-r--r--    1 root     root          2251 Jan 19 14:05 package.json
#23 0.350 drwxr-xr-x    2 root     root          4096 Jan 19 14:05 plugins
#23 0.350 -rw-r--r--    1 root     root            51 Jan 19 14:05 postcss.config.mjs
#23 0.350 drwxr-xr-x    3 root     root          4096 Jan 19 14:05 public
#23 0.350 drwxr-xr-x    3 root     root          4096 Jan 19 14:05 secrets
#23 0.350 -rw-r--r--    1 root     root          1461 Jan 19 14:05 sentry.client.config.ts
#23 0.350 -rw-r--r--    1 root     root           925 Jan 19 14:05 sentry.edge.config.ts
#23 0.350 -rw-r--r--    1 root     root           934 Jan 19 14:05 sentry.server.config.ts
#23 0.350 drwxr-xr-x    9 root     root          4096 Jan 19 14:05 src
#23 0.350 -rw-r--r--    1 root     root           445 Jan 19 14:05 tsconfig.json
So...you are telling me that
    "build": {
      "dependsOn": ["^build"],
      "env": [
        "NODE_ENV",
        "NEXT_PUBLIC_*",
        "VITE_*",
        "SENTRY_AUTH_TOKEN",
        "TURBO_TEAM",
        "TURBO_TOKEN",
        "APP_ENV",
        "APP_URL"
      ],
      "inputs": ["$TURBO_DEFAULT$", ".env*"],
      "outputs": [".next/**", "!.next/cache/**", "dist/**"]
    },
    "codehouse#build": {
      "dependsOn": ["@repo/runtime-env#build", "^build"]
    },

the "codehouse#build" is overriding the "build".
Oh that is just awful. I mean I understand it....I just didn't think of it
Answer