Vite React on Railway with Docker+Caddy isn't getting VITE_ env vars at build time.
rn_brand
HOBBYOP

3 months ago

I'm deploying a Vite + React application on Railway using the official guide here:
https://docs.railway.com/guides/react

Railway automatically detected a Dockerfile, so the app is deployed using Docker + Caddy (as in the example from the guide).

Everything works except one issue:

Environment variables with the VITE_ prefix are not appearing in the built frontend.

So, what is the correct way to deploy a react app in railway?

Solved$10 Bounty

Pinned Solution

Try adding ARG and ENV in your Dockerfile so that it can access the variables during build time.

Eg: if the variable was named VITE_BACKEND_URL , add the following lines before the build command:

# FROM:...

# Add these two lines:
ARG VITE_BACKEND_URL
ENV VITE_BACKEND_URL

WORKDIR /app

# ...

3 Replies

Try adding ARG and ENV in your Dockerfile so that it can access the variables during build time.

Eg: if the variable was named VITE_BACKEND_URL , add the following lines before the build command:

# FROM:...

# Add these two lines:
ARG VITE_BACKEND_URL
ENV VITE_BACKEND_URL

WORKDIR /app

# ...

0x5b62656e5d

Try adding ARG and ENV in your Dockerfile so that it can access the variables during build time.Eg: if the variable was named VITE_BACKEND_URL , add the following lines before the build command:# FROM:... # Add these two lines: ARG VITE_BACKEND_URL ENV VITE_BACKEND_URL WORKDIR /app # ...

rn_brand
HOBBYOP

3 months ago

So, railway's variable adding option wont help? Always i have to add the env in the dockerfile?


It’s not that it won’t help, you need to allow the Docker build process to be able to access the variables.

And when you add the env to Dockerfile, only put the names of the variable (as shown in example above). Do not ever commit environment variables.


Status changed to Solved brody 3 months ago


Loading...