Docker application not finding env var set in service's Variables tab

Project ID: 21dcd679-2ad7-463a-81e8-ed549f5d467b I have a dockerized Vite application that needs to read a var VITE_MY_ENV_VAR that i've set to value 'the-value' in the Variables tab of my Railway app's service. However, the application cannot find the variable during run time (printing it's value with console.log returns undefined). The application does successfully read the PORT variable automatically set by Railway both at build time (during an EXPOSE $PORT step) and runtime (during gunicorn --host 0.0.0.0 --port $PORT app:app). What might be causing my application to not find the VITE_MY_ENV_VAR variable?
Solution:
between line 11 and 12 you would need to specify the VITE_ variables your app needs like this ARG VITE_MY_ENV_VAR
Jump to solution
9 Replies
Percy
Percy9mo ago
Project ID: 21dcd679-2ad7-463a-81e8-ed549f5d467b
0x5CD59377
0x5CD593779mo ago
also, my application works fine if i build and run the dockerfile locally
Brody
Brody9mo ago
may i ask what gunicorn has to do with vite?
0x5CD59377
0x5CD593779mo ago
oh sorry. vite is being built to yarn build and then served with gunicorn
# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/

# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7

ARG PYTHON_VERSION=3.11.7
ARG NODE_VERSION=18.17.1
FROM node:${NODE_VERSION}-alpine as build
WORKDIR /app
# RUN --mount=type=bind,source=package.json,target=package.json \
# --mount=type=bind,source=yarn.lock,target=yarn.lock \
# yarn install
COPY package.json yarn.lock ./
RUN yarn install
COPY . .
RUN yarn build

FROM python:${PYTHON_VERSION}-slim as base

# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1

# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1

# Railway.app specifies the port we should listen on
# We default to 8000 in a local environment
ENV PORT=8000

WORKDIR /app

# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
# Leverage a bind mount to requirements.txt to avoid having to copy them into
# into this layer.
RUN apt update && apt install -y --no-install-recommends libmagic-dev build-essential && rm -rf /var/lib/apt/lists/*
RUN pip install poetry

# RUN --mount=type=cache,id=s/2e9c673d-c965-440e-81a8-f03b0ad4a826-/pip-cache,target=/root/.cache/pip \
# --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
# --mount=type=bind,source=poetry.lock,target=poetry.lock \
# poetry export -f requirements.txt --output requirements.txt && python -m pip install -r requirements.txt
COPY pyproject.toml poetry.lock ./
RUN poetry export -f requirements.txt --output requirements.txt && python -m pip install -r requirements.txt

# Switch to the non-privileged user to run the application.
USER appuser

# Copy the source code into the container.
COPY --from=build /app/build /app/build
COPY ./app /app/app
COPY ./public /app/public
# COPY .env /app/.env

# Expose the port that the application listens on.
EXPOSE $PORT

# Run the application.
CMD gunicorn app:app
# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/

# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7

ARG PYTHON_VERSION=3.11.7
ARG NODE_VERSION=18.17.1
FROM node:${NODE_VERSION}-alpine as build
WORKDIR /app
# RUN --mount=type=bind,source=package.json,target=package.json \
# --mount=type=bind,source=yarn.lock,target=yarn.lock \
# yarn install
COPY package.json yarn.lock ./
RUN yarn install
COPY . .
RUN yarn build

FROM python:${PYTHON_VERSION}-slim as base

# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1

# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1

# Railway.app specifies the port we should listen on
# We default to 8000 in a local environment
ENV PORT=8000

WORKDIR /app

# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
# Leverage a bind mount to requirements.txt to avoid having to copy them into
# into this layer.
RUN apt update && apt install -y --no-install-recommends libmagic-dev build-essential && rm -rf /var/lib/apt/lists/*
RUN pip install poetry

# RUN --mount=type=cache,id=s/2e9c673d-c965-440e-81a8-f03b0ad4a826-/pip-cache,target=/root/.cache/pip \
# --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
# --mount=type=bind,source=poetry.lock,target=poetry.lock \
# poetry export -f requirements.txt --output requirements.txt && python -m pip install -r requirements.txt
COPY pyproject.toml poetry.lock ./
RUN poetry export -f requirements.txt --output requirements.txt && python -m pip install -r requirements.txt

# Switch to the non-privileged user to run the application.
USER appuser

# Copy the source code into the container.
COPY --from=build /app/build /app/build
COPY ./app /app/app
COPY ./public /app/public
# COPY .env /app/.env

# Expose the port that the application listens on.
EXPOSE $PORT

# Run the application.
CMD gunicorn app:app
Brody
Brody9mo ago
not the best practice, but it works
0x5CD59377
0x5CD593779mo ago
here's my dockerfile
Solution
Brody
Brody9mo ago
between line 11 and 12 you would need to specify the VITE_ variables your app needs like this ARG VITE_MY_ENV_VAR
0x5CD59377
0x5CD593779mo ago
i'll give this a shot it worked! thanks so much :)
Brody
Brody9mo ago
no problem!
Want results from more Discord servers?
Add your server