最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - turborepo NEXT_PUBLIC build variables not correctly used in next.js when passing over docker - Stack Overflow

programmeradmin4浏览0评论

I somehow cant access my build variables on my next.js app when i pass them in the docker compose file. When i print the process.env in the docker environment It is correctly configured and set but it is not the correct value in the next js environment, when i call the scripts. i dont want to use .env files in production, there are getting ignored over the dockerignore, i want to pass them into the process.env!

iam calling the variable in a client script like that ;

process.env.NEXT_PUBLIC_API_URL

my docker compose

services:
  web:
    build:
      context: .
      dockerfile: ./apps/web/Dockerfile
    restart: always
    environment:
      - NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}
      ...

Dockerfile of my next.js web app

FROM node:22-alpine AS base

FROM base AS builder
# Check  to understand why libc6-compat might be needed.
RUN apk update
RUN apk add --no-cache libc6-compat
# Set working directory
WORKDIR /app
RUN npm install -g turbo
COPY . .
RUN turbo prune web --docker

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
RUN apk update
RUN apk add --no-cache libc6-compat
WORKDIR /app

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

# Build the project
COPY --from=builder /app/out/full/ .

RUN npx turbo run build

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

# Automatically leverage output traces to reduce image size
# 
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

CMD node apps/web/server.js

thats my root turbo.json;

{
  "$schema": "/schema.json",
  "tasks": {
    "build": {
      "inputs": ["$TURBO_DEFAULT$", ".env*"],
      "outputs": ["dist/**", ".next/**", "!.next/cache/**", "public/dist/**"],
      "dependsOn": ["^build"]
    },
    ...
  }
}

That is what I tried;

  • using a part of the web project in standalone environment without turborepo and docker-compose. The variable was correctly set in the next.js environment there
  • pass NEXT_PUBLIC_API_URL in the Dockerfile as a ARG
  • set NEXT_PUBLIC_API_URL in globalEnv, build env and web#build in the turbo.json like this this
  • set NEXT_PUBLIC_API_URL in the next.config.ts like this
  • start the turbo build with turbo run build --env-mode=loose
  • created a new turbo.json in the web project by inheriting the root config and setting the env´s

but still, next js is not picking up the correct value from process.env. it seems like the variable is static and cant be set...

as you can see, the docker compose env is correctly set, but next.js is not picking it up....

I somehow cant access my build variables on my next.js app when i pass them in the docker compose file. When i print the process.env in the docker environment It is correctly configured and set but it is not the correct value in the next js environment, when i call the scripts. i dont want to use .env files in production, there are getting ignored over the dockerignore, i want to pass them into the process.env!

iam calling the variable in a client script like that ;

process.env.NEXT_PUBLIC_API_URL

my docker compose

services:
  web:
    build:
      context: .
      dockerfile: ./apps/web/Dockerfile
    restart: always
    environment:
      - NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}
      ...

Dockerfile of my next.js web app

FROM node:22-alpine AS base

FROM base AS builder
# Check https://github/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 npm install -g turbo
COPY . .
RUN turbo prune web --docker

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
RUN apk update
RUN apk add --no-cache libc6-compat
WORKDIR /app

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

# Build the project
COPY --from=builder /app/out/full/ .

RUN npx turbo run build

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

# Automatically leverage output traces to reduce image size
# https://nextjs./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

CMD node apps/web/server.js

thats my root turbo.json;

{
  "$schema": "https://turbo.build/schema.json",
  "tasks": {
    "build": {
      "inputs": ["$TURBO_DEFAULT$", ".env*"],
      "outputs": ["dist/**", ".next/**", "!.next/cache/**", "public/dist/**"],
      "dependsOn": ["^build"]
    },
    ...
  }
}

That is what I tried;

  • using a part of the web project in standalone environment without turborepo and docker-compose. The variable was correctly set in the next.js environment there
  • pass NEXT_PUBLIC_API_URL in the Dockerfile as a ARG
  • set NEXT_PUBLIC_API_URL in globalEnv, build env and web#build in the turbo.json like this this
  • set NEXT_PUBLIC_API_URL in the next.config.ts like this
  • start the turbo build with turbo run build --env-mode=loose
  • created a new turbo.json in the web project by inheriting the root config and setting the env´s

but still, next js is not picking up the correct value from process.env. it seems like the variable is static and cant be set...

as you can see, the docker compose env is correctly set, but next.js is not picking it up.... https://github/vercel/turborepo/discussions/10245

Share Improve this question edited Apr 1 at 9:31 tuke307 asked Mar 31 at 18:08 tuke307tuke307 4405 silver badges13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

this solved the issue. after hours of debugging;

in the docker-compose, moving from environment to build env like mentioned in the Next.js sample repo

services:
  web:
    build:
      context: .
      dockerfile: ./apps/web/Dockerfile
      args:
        - NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}
    restart: always
      ...

then passing the build env´s to every image in the Dockerfile of my next.js web app;

FROM node:22-alpine AS base

# This Dockerfile is copy-pasted into our main docs at /docs/handbook/deploying-with-docker.
# Make sure you update both files!

FROM base AS builder
# Check https://github/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 npm install -g turbo
COPY . .

# Environment variables must be present at build time
ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}

RUN turbo prune web --docker

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
RUN apk update
RUN apk add --no-cache libc6-compat
WORKDIR /app

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

# Build the project
COPY --from=builder /app/out/full/ .

# Environment variables must be present at build time
ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}

RUN npx turbo run build

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 --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

# Environment variables must be redefined at run time
ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}

CMD node apps/web/server.js

then it actually works. this was not mentioned in the turborepo docs though.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论