R
Railway•14mo ago
p1gp3n

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
Percy
Percy•14mo ago
Project ID: ac828fce-6f54-43e9-825c-ac4871d226ba
Brody
Brody•14mo ago
your app is using docker or using a Dockerfile?
p1gp3n
p1gp3n•14mo ago
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
Brody
Brody•14mo ago
then you would need to modify your Dockerfile to include that flag
p1gp3n
p1gp3n•14mo ago
i tried passing in ARG RAILWAY_ENVIRONMENT. is this how railway recommends to do this?
p1gp3n
p1gp3n•14mo ago
i tried this but ran into some issues. let me try again and i can let you know if there are specific errors.
Brody
Brody•14mo ago
sounds good
p1gp3n
p1gp3n•14mo ago
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}
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?
Brody
Brody•14mo ago
ENV RAILWAY_ENVIRONMENT=$RAILWAY_ENVIRONMENT
ENV RAILWAY_ENVIRONMENT=$RAILWAY_ENVIRONMENT
no curly braces afaik
p1gp3n
p1gp3n•14mo ago
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
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
RUN nxp vite build --mode staging
and that will work.
Brody
Brody•14mo ago
okay I will do some experiments a bit later and get back to you
p1gp3n
p1gp3n•14mo ago
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.
Brody
Brody•14mo ago
also, what use a dockerfile? it doesn't look like your dockerfile is doing anything nixpacks wouldn't already do
p1gp3n
p1gp3n•14mo ago
just for basic portability purposes, not for railway in particular.
Brody
Brody•14mo ago
ah okay totally valid will get back to you!
p1gp3n
p1gp3n•14mo ago
sweet- thanks :). also: we use docker-compose with the api (also hosted on railway).
Brody
Brody•14mo ago
railway doesn't support docker compose?
p1gp3n
p1gp3n•14mo ago
no- just locally to run both parts of the app. which is why we use docker for the frontend
Brody
Brody•14mo ago
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
p1gp3n
p1gp3n•14mo ago
RAILWAY_ENVIRONMENT is wrong?
Brody
Brody•14mo ago
you spelt railway without the i woah wtf is wrong with me, okay I gotta take a break
p1gp3n
p1gp3n•14mo ago
ha yes i tried to copy/paste the correct spelling in and re-push, but i cannot git commit because there are no changes 🙂
Brody
Brody•14mo ago
ARG RAILWAY_ENVIRONMENT must be placed after the FROM derivative
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
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
this is wrong for some reason
p1gp3n
p1gp3n•14mo ago
ill give it a try. yeah i was just following the instructions 🙂
Brody
Brody•14mo ago
so you have a railway environment called staging and have modifyed your vite config to build your app differently if the mode == staging?
p1gp3n
p1gp3n•14mo ago
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
Brody
Brody•14mo ago
yep already talked with the appropriate people, and I will be updating the docs with examples and where to put ARG
p1gp3n
p1gp3n•14mo ago
realy helpful, thanks a lot. i would have been stuck on this for a while.
Brody
Brody•14mo ago
no problem!