next.js: Environment variable undefined in next.config.ts when running in Docker container

Link to the code that reproduces this issue

https://github.com/kuanjiahong/env-var-not-defined-docker

To Reproduce

  1. Start the container with docker compose up --build
  2. Go to http://localhost:3000/
  3. Click the link

Image

Current vs. Expected behavior

Current behaviour:

You are redirected to /fail-to-rewrite which means process.env.REWRITE_URL is undefined. This logic comes from the config at next.config.ts

Screenshot showing the rewrite logic in next.config.ts Image

Current page: Image

Expected behaviour:

You should be redirected to https://example.com/ as defined in the docker-compose.yaml file in the REWRITE_URL environment variable

Screenshot showing the location where REWRITE_URL is defined in docker-compose.yaml Image

Expected page:

Image

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Enterprise
  Available memory (MB): 32562
  Available CPU cores: 8
Binaries:
  Node: 22.12.0
  npm: 11.0.0
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 15.3.0-canary.24 // Latest available version is detected (15.3.0-canary.24).
  eslint-config-next: N/A
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.8.2
Next.js Config:
  output: standalone

Which area(s) are affected? (Select all that apply)

Runtime

Which stage(s) are affected? (Select all that apply)

next start (local)

Additional context

I found this issue when I run my project with Docker.

Here are my Docker information:

Client:
 Version:           28.0.1
 API version:       1.48
 Go version:        go1.23.6
 Git commit:        068a01e
 Built:             Wed Feb 26 10:41:52 2025
 OS/Arch:           windows/amd64
 Context:           desktop-linux

Server: Docker Desktop 4.39.0 (184744)
 Engine:
  Version:          28.0.1
  API version:      1.48 (minimum version 1.24)
  Go version:       go1.23.6
  Git commit:       bbd0a17
  Built:            Wed Feb 26 10:41:16 2025
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.25
  GitCommit:        bcc810d6b9066471b0b6fa75f557a15a1cbf31bb
 runc:
  Version:          1.2.4
  GitCommit:        v1.2.4-0-g6c52b3f
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

When inspecting the environment variables in the container. The environment variable REWRITE_URL exist

Image

These issues might be related: https://github.com/vercel/next.js/issues/77436 https://github.com/vercel/next.js/issues/77448

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 5
  • Comments: 17 (1 by maintainers)

Most upvoted comments

As of version 15.3.0 and later, the NEXT_PUBLIC_* variables are being compiled to window.NEXT_PUBLIC_* on the browser side, which renders them apparently undefined.

I use turborepo along with a standalone env package that depends on @t3-oss/env-nextjs, and build it using a Dockerfile in a standalone output mode. Has anyone else encountered this issue?

I haven’t been able to create a minimal reproduction, but when I revert to next 15.2.4, the problem disappears.

@Ve2s4 what issue?

Missing env variables in Docker. I had to pass them as ARGs and then set them in the image’s env vars, which poses a security issue, I know. It’s a workaround I found somewhere, but seriously, why does Next.js handle env vars like it’s trying to win an award for absurdity.

@kuanjiahong the config is only ran once at compile time, if you want dynamic rewrites just use middleware

https://nextjs.org/docs/app/api-reference/functions/next-response#rewrite

@kuanjiahong i will add, you can check here /app/.next/routes-manifest.json this is created during build and is where the rewrites are defined.

@kuanjiahong it only looked like it worked probably because the route had been cached by your browser, if you did a full refresh you would find it hadn’t worked, unless you have a .env file in your build directory

passing it in as an env during runtime does nothing, as it is compiled during build time.

@kuanjiahong the env needs to be present during build, so either a .env file needs to be present in the build directory, or you pass it in as a build arg, and then read it using the arg command

@kuanjiahong yes sorry in your dockerfile you would need to add:

ARG REWRITE_URL

in your build stage, before the build command

@djsisson I have tried and it still does not work. For context, I only changed the docker-compose.yaml. I did not update the Dockerfile to read the args from the docker-compose.yaml

@kuanjiahong can you add it as a build arg

services:
  frontend:
    build: 
      context: .
      args:
        - REWRITE_URL=https://www.example.com
    ports: "3000:3000"