Vite + Docker deployments to non-Production (staging) environments
I can build my Vite (React) site locally with Staging Environment configs by using the "vite build --mode staging" in my package.json script. Vite automatically uses 'production' mode with the 'build' command unless you specify otherwise. Since my app is using Docker, I want to specify the build command using a railway.toml file, but it seems build commands are only supported with Nixpacks not Docker (this is the message I get during the build). So my Railway app on Staging is incorrectly built with Prod configs. ProjectID: ac828fce-6f54-43e9-825c-ac4871d226ba
30 Replies
Project ID:
ac828fce-6f54-43e9-825c-ac4871d226ba
your app is using docker or using a Dockerfile?
its using Dockerfile to build on Railway
locally, i can say "vite build --mode staging, and it will build with my staging configs. but i can't do this on Railway bc its using Dockerfile build not Nixpack
then you would need to modify your Dockerfile to include that flag
i tried passing in ARG RAILWAY_ENVIRONMENT. is this how railway recommends to do this?
yes that would be how its done
https://docs.railway.app/deploy/dockerfiles#environment-variables
i tried this but ran into some issues. let me try again and i can let you know if there are specific errors.
sounds good
i'm trying to do this, but the echo is coming back null, so i must have something wrong w/the syntax?
no curly braces afaik
still getting an error. the echo returns null still so i'm sure thats why its failing.
i can hard-code and that will work.
i can hard-code and that will work.
okay I will do some experiments a bit later and get back to you
cool- thanks. yeah it seems its not related to vite. just the way the RAILWAY_ENVIRONMENT is being passed, and im not really sure how that needs to be done.
also, what use a dockerfile? it doesn't look like your dockerfile is doing anything nixpacks wouldn't already do
just for basic portability purposes, not for railway in particular.
ah okay totally valid
will get back to you!
sweet- thanks :). also: we use docker-compose with the api (also hosted on railway).
railway doesn't support docker compose?
no- just locally to run both parts of the app. which is why we use docker for the frontend
gotcha
oh um I may know what went wrong
you spelt railway wrong in the ARG variable
sure is easy to miss a little typo like that
RAILWAY_ENVIRONMENT is wrong?
ha
yes i tried to copy/paste the correct spelling in and re-push, but i cannot git commit because there are no changes 🙂
ARG RAILWAY_ENVIRONMENT
must be placed after the FROM
derivative
this is wrong for some reasonill give it a try. yeah i was just following the instructions 🙂
so you have a railway environment called staging and have modifyed your vite config to build your app differently if the mode == staging?
im pretty new to vite, but from what i understand, it will use your .env.[environment] file during build, and defaults to .env.production for all builds unless you specify another config. so we have config variables in our .env.staging file that we need, but during staging builds our .env.production configs were being used bc we couldn't specifify the build mode.
im still doing some testing, but it looks like the env is being pulled in corectly now 🙂 thanks! yeah def update the docs if you can, im sure others prob had that problem
yep already talked with the appropriate people, and I will be updating the docs with examples and where to put ARG
realy helpful, thanks a lot. i would have been stuck on this for a while.
no problem!