R

Railway

βœ‹ο½œhelp

Join Server

Vite + Docker deployments to non-Production (staging) environments

Pp1gp3n5/19/2023
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
Bbrody5/19/2023
your app is using docker or using a Dockerfile?
Pp1gp3n5/19/2023
its using Dockerfile to build on Railway
Pp1gp3n5/19/2023
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
Bbrody5/19/2023
then you would need to modify your Dockerfile to include that flag
Pp1gp3n5/19/2023
i tried passing in ARG RAILWAY_ENVIRONMENT. is this how railway recommends to do this?
Bbrody5/19/2023
yes that would be how its done
Pp1gp3n5/19/2023
i tried this but ran into some issues. let me try again and i can let you know if there are specific errors.
Bbrody5/19/2023
sounds good
Pp1gp3n5/19/2023
ARG RAILWAY_ENVIRONMENT

FROM node:18 as build

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

ENV RAILWAY_ENVIRONMENT=${RAILWAY_ENVIRONMENT}

RUN echo $RAILWAY_ENVIRONMENT

RUN npx vite build --mode ${RAILWAY_ENVIRONMENT}


i'm trying to do this, but the echo is coming back null, so i must have something wrong w/the syntax?
Bbrody5/19/2023
ENV RAILWAY_ENVIRONMENT=$RAILWAY_ENVIRONMENT
Bbrody5/19/2023
no curly braces afaik
Pp1gp3n5/19/2023
still getting an error. the echo returns null still so i'm sure thats why its failing.

ERROR: process "/bin/sh -c npx vite build --mode $RAILWAY_ENVIRONMENT" did not complete successfully: exit code: 1


i can hard-code
RUN nxp vite build --mode staging
and that will work.
Bbrody5/19/2023
okay I will do some experiments a bit later and get back to you
Pp1gp3n5/19/2023
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.
Bbrody5/19/2023
also, what use a dockerfile? it doesn't look like your dockerfile is doing anything nixpacks wouldn't already do
Pp1gp3n5/19/2023
just for basic portability purposes, not for railway in particular.
Bbrody5/19/2023
ah okay totally valid
Bbrody5/19/2023
will get back to you!
Pp1gp3n5/19/2023
sweet- thanks :). also: we use docker-compose with the api (also hosted on railway).
Bbrody5/19/2023
railway doesn't support docker compose?
Pp1gp3n5/19/2023
no- just locally to run both parts of the app. which is why we use docker for the frontend
Bbrody5/19/2023
gotcha
Bbrody5/19/2023
oh um I may know what went wrong
Bbrody5/19/2023
you spelt railway wrong in the ARG variable
Bbrody5/19/2023
sure is easy to miss a little typo like that
Pp1gp3n5/19/2023
RAILWAY_ENVIRONMENT is wrong?
Bbrody5/19/2023
you spelt railway without the i
Bbrody5/19/2023
woah wtf is wrong with me, okay I gotta take a break
Pp1gp3n5/19/2023
ha
Pp1gp3n5/19/2023
yes i tried to copy/paste the correct spelling in and re-push, but i cannot git commit because there are no changes πŸ™‚
Bbrody5/19/2023
ARG RAILWAY_ENVIRONMENT must be placed after the FROM derivative
Bbrody5/19/2023
FROM node:18 as build

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

ARG RAILWAY_ENVIRONMENT
ENV RAILWAY_ENVIRONMENT=$RAILWAY_ENVIRONMENT

RUN echo $RAILWAY_ENVIRONMENT

RUN npx vite build --mode $RAILWAY_ENVIRONMENT
Bbrody5/19/2023
this is wrong for some reason
Pp1gp3n5/19/2023
ill give it a try. yeah i was just following the instructions πŸ™‚
Bbrody5/19/2023
so you have a railway environment called staging and have modifyed your vite config to build your app differently if the mode == staging?
Pp1gp3n5/19/2023
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.
Pp1gp3n5/19/2023
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
Bbrody5/19/2023
yep already talked with the appropriate people, and I will be updating the docs with examples and where to put ARG
Pp1gp3n5/19/2023
realy helpful, thanks a lot. i would have been stuck on this for a while.
Bbrody5/19/2023
no problem!