Docker Build Not Receiving Environment Variables as ARGs Despite railway.toml Configuration

applezebra
FREE

15 days ago

I'm deploying a Next.js 14 app that requires NEXT_PUBLIC_* environment

variables during build time (as Next.js inlines these into the JavaScript

bundle).

Issue:

Environment variables set in Railway dashboard are not being passed as Docker

build arguments, causing build failure.

Error:

Missing NEXT_PUBLIC_SUPABASE_URL environment variable

Current Setup:

1. Environment variables are set in Railway dashboard (verified)

2. Using custom Dockerfile with ARG directives:

ARG NEXT_PUBLIC_SUPABASE_URL

ARG NEXT_PUBLIC_SUPABASE_ANON_KEY

ENV NEXT_PUBLIC_SUPABASE_URL=$NEXT_PUBLIC_SUPABASE_URL

ENV NEXT_PUBLIC_SUPABASE_ANON_KEY=$NEXT_PUBLIC_SUPABASE_ANON_KEY

3. railway.toml configured to use Dockerfile:

[build]

builder = "dockerfile"

dockerfilePath = "./Dockerfile"

What I've Tried:

- Initially used Nixpacks (failed - env vars not available at build)

- Switched to Dockerfile with ARG directives (still failing)

- Added lazy initialization pattern (build still requires env vars)

- Verified variables exist in Railway dashboard

Question:

How can I make Railway pass environment variables as Docker build arguments? Is

there additional configuration needed beyond the ARG directives in Dockerfile?

Repository:github.com/applezebra/mcpguard

Framework: Next.js 14.2.5 with App Router

$10 Bounty

5 Replies

Railway
BOT

15 days ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


Railway

Hey there! We've found the following might help you get unblocked faster: - [🧵 Build environment variables](https://station.railway.com/questions/build-environment-variables-9ca6b8f4) - [🧵 Pass environment variables to Docker directly](https://station.railway.com/feedback/pass-environment-variables-to-docker-dir-14c05e65) - [🧵 Build failing due to missing environment variables](https://station.railway.com/questions/build-failing-due-to-missing-environment-4afb7fdb) - [🧵 Environment Variables in ViteJS Not Available at Build Time on Railway](https://station.railway.com/questions/environment-variables-in-vite-js-not-avai-a4085c8d) If you find the answer from one of these, please let us know by solving the thread!

applezebra
FREE

15 days ago

not yet, still can't get railway to read the variables


14 days ago

This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.

Status changed to Open brody 14 days ago


clashing
FREETop 1% Contributor

3 days ago

Why do you need to pass the environment variables in the Dockerfile itself? Anything specific?

Because you can skip that, and add those after you import your project in Railway (right before deploying it), via the VARIABLES tab. And then could use the regular ENVIRONMENT setup in your server file.

Service variables are what you might need: https://docs.railway.com/guides/variables

For instance, you can do this in a node.js server:

let testVar = process.env.TEST_VAR || undefined;

You can do that easily in your choice of language,which would bypass those errors


clashing
FREETop 1% Contributor

2 days ago

Did that helped?


clashing

Why do you need to pass the environment variables in the Dockerfile itself? Anything specific? Because you can skip that, and add those after you import your project in Railway (right before deploying it), via the VARIABLES tab. And then could use the regular ENVIRONMENT setup in your server file.Service variables are what you might need: https://docs.railway.com/guides/variablesFor instance, you can do this in a node.js server:let testVar = process.env.TEST_VAR || undefined;You can do that easily in your choice of language,which would bypass those errors

clashing
FREETop 1% Contributor

16 hours ago

Any progress, applezebra?

If it worked for you, please mark my initial post as the solution for this query!


Docker Build Not Receiving Environment Variables as ARGs Despite railway.toml Configuration - Railway Help Station