Help with deploying a react + vite app on railway using docker
I am in need of help in regards to deploying a react + vite app to railway. So I have a dockerfile for my app which i've told railway the path to the dockerfile to deploy the app. The issue is that from what I've gathered, railway doesn't inject the ENV variables during the build process and vite relies on that as it takes ENV variables then replaces the values in the code during the build process.
Is there a way of passing ARG variables as an alternative in railway to my dockerfile? Or what would be the solution for me to be able to use the ENV variables during the build process so that Vite has access to the ENV variables to inject into the code?
Just for some context this is a bit of the code in the dockerfile that I'm referring to:
19 Replies
Project ID:
N/A
n/a
thats an incomplete dockerfile, do you mind sending the full file?
Yes sure
I am using turborepo
thats why i need to prune the webapp project and build it in this way as it has dependencies with another package in the monorepo
just a note this dockerfile builds perfectly fine and it works locally when i pass it a env_file to the docker-compose process etc
okay so for example if you needed the
DATABASE_URL
variable during build all you need to do is define it with the ARG
keyword in the same image layer that will be using that variable, like so
Will this work on railway?
yes
ok one sec let me try that
This is where i've declared my ENV variables
instead of
Okay so the deployment was successful this time
I just realised that my build command was the issue. I had this command for the npm run build script:
doppler run -- tsc && vite build
the issue was that the doppler variables were not being injected into the vite build process, it seems like it was being injected during the tsc command process
after i switched it to: tsc && doppler run -- vite build
it started working
however on railway i'm getting a server error when i try to open the webapp
its being hosted via nginxhave nginx listen on 8080
and then set a railway service variable PORT = 8080
just saw that the port ENV variable was missing
its only needed if your app doesnt listen on $PORT by default, since railway will generate one for your app internally
ahh i see
deployment in progress right now
i set nginx to listen on 3000 since my react app listens on 3000 so lets see if this works
as long as you set PORT to 3000
and look at that! The app is live on Railway 🥳
Thank you so much @Brody , really appreciate the very quick responses and help 👍
no problem 🙂