-
Notifications
You must be signed in to change notification settings - Fork 28.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Environment variable undefined in next.config.ts when running in Docker container #77539
Comments
@kuanjiahong can you add it as a build arg services:
frontend:
build:
context: .
args:
- REWRITE_URL=https://www.example.com
ports: "3000:3000" |
@djsisson I have tried and it still does not work. For context, I only changed the |
@kuanjiahong yes sorry in your dockerfile you would need to add:
in your build stage, before the build command |
@djsisson Great, your solution works. However, I wonder if there is a way to do it without using For context, this is my edited Dockerfile
|
@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 |
@djsisson Understood. Based on your explanation, I decided to see what is the behaviour if I build the image first and run the Here is the workflow:
This will solve the issue and we do not need to include (UPDATE 29/03/2025: This worfklow does not solve the issue. It worked because apparently the browser cached the route) On the other hand, if I do it this way:
This does not solve the issue as the application somehow will not be able to read the environment variable |
@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 i will add, you can check here |
This issue is such a PIA. I’ve been trying to solve it for three days now :( |
@Ve2s4 what issue? |
@djsisson You are right. I tried using another browser (Mozilla Firefox) and the env was not defined. |
@djsisson I got to learn something new. To see this, I ran (1) with an empty .env file in the build directory and this is the result (2) .env file with REWRITE_URL=https://example.com So it seems that |
@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 |
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. |
@Ve2s4 you don't need to set build vars as env vars, also you can pass in as docker secrets if you don't want to see them. |
Link to the code that reproduces this issue
https://github.com/kuanjiahong/env-var-not-defined-docker
To Reproduce
docker compose up --build
Current vs. Expected behavior
Current behaviour:
You are redirected to
/fail-to-rewrite
which meansprocess.env.REWRITE_URL
is undefined. This logic comes from the config atnext.config.ts
Screenshot showing the rewrite logic in

next.config.ts
Current page:

Expected behaviour:
You should be redirected to https://example.com/ as defined in the
docker-compose.yaml
file in theREWRITE_URL
environment variableScreenshot showing the location where

REWRITE_URL
is defined indocker-compose.yaml
Expected page:
Provide environment information
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:
When inspecting the environment variables in the container. The environment variable
REWRITE_URL
existThese issues might be related:
#77436
#77448
The text was updated successfully, but these errors were encountered: