10 months ago
When I run my docker file deployment, my environment variables defined within railway are not accessible in my settings.py file. I get the following error:
"""
File "/opt/venv/lib/python3.12/site-packages/decouple.py", line 92, in get
raise UndefinedValueError('{} not found. Declare it as envvar or define a default value.'.format(option))
decouple.UndefinedValueError: stripe_public_key not found. Declare it as envvar or define a default value.
"""
(Note stripe_public_key is only an example and it is the case for all env. variables)
I've also used """ os.envrion.get('stripe_public_key') """ and I still get the same value. It works on my local machine but just not railway.
7 Replies
10 months ago
Hey there! We've found the following might help you get unblocked faster:
- 🧵 Issue regarding environment variables
- 📚 Environment Variables
- 🧵 Environment variables are undefined during runtime Turborepo
If you find the answer from one of these, please let us know by solving the thread!
10 months ago
Why this error happens
You set environment variables in Railway, but your app can't see them because they're not automatically included in the Docker image unless you do it the right way.
Locally, .env works with python-decouple, but Railway doesn't use your .env unless you copy it in — which you shouldn’t.
Option 1: Add variables in Dockerfile (build-time)
Use this if your app needs the variables during the Docker build process.
Dockerfile:
FROM python:3.12-slim
WORKDIR /app
# Declare args
ARG STRIPE_PUBLIC_KEY
ARG STRIPE_SECRET_KEY
# Map args to env vars
ENV STRIPE_PUBLIC_KEY=$STRIPE_PUBLIC_KEY
ENV STRIPE_SECRET_KEY=$STRIPE_SECRET_KEY
# Install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["gunicorn", "myproject.wsgi"]
Railway automatically injects values for these ARG variables when deploying.
Option 2: Use Railway runtime environment variables (recommended)
- Remove this line from your Dockerfile if it exists:
COPY .env .- In your
settings.py, load environment variables like this:
from decouple import config
STRIPE_PUBLIC_KEY = config('STRIPE_PUBLIC_KEY')
STRIPE_SECRET_KEY = config('STRIPE_SECRET_KEY')
- In Railway:
- Go to your service
- Add these under the Variables section:
STRIPE_PUBLIC_KEY=your_value
STRIPE_SECRET_KEY=your_value - Click Apply Changes and Redeploy
Summary
- If you need variables during the build: use
ARG+ENVin the Dockerfile - If your app only needs them when running: just use Railway's runtime injection (recommended)
Make sure to click Apply Changes and Redeploy after updating variables in Railway.
10 months ago
Additional Context to my issue:
I have placed the appropriate variables into my Railway Variables and have redeployed. When checking my details, I can see the variables in the details tab.
loganbek
## Why this error happens You set environment variables in Railway, but your app can't see them because they're not automatically included in the Docker image unless you do it the right way. Locally, `.env` works with `python-decouple`, but Railway doesn't use your `.env` unless you copy it in — which you shouldn’t. ## Option 1: Add variables in Dockerfile (build-time) Use this if your app needs the variables during the Docker build process. **Dockerfile:** ``` FROM python:3.12-slim WORKDIR /app # Declare args ARG STRIPE_PUBLIC_KEY ARG STRIPE_SECRET_KEY # Map args to env vars ENV STRIPE_PUBLIC_KEY=$STRIPE_PUBLIC_KEY ENV STRIPE_SECRET_KEY=$STRIPE_SECRET_KEY # Install dependencies COPY requirements.txt . RUN pip install -r requirements.txt COPY . . CMD ["gunicorn", "myproject.wsgi"] ``` Railway automatically injects values for these `ARG` variables when deploying. ## Option 2: Use Railway runtime environment variables (recommended) 1. Remove this line from your Dockerfile if it exists: ``` COPY .env . ``` 1. In your `settings.py`, load environment variables like this: ``` from decouple import config STRIPE_PUBLIC_KEY = config('STRIPE_PUBLIC_KEY') STRIPE_SECRET_KEY = config('STRIPE_SECRET_KEY') ``` 1. In Railway: * Go to your service * Add these under the **Variables** section: ``` STRIPE_PUBLIC_KEY=your_value STRIPE_SECRET_KEY=your_value ``` 1. Click **Apply Changes** and **Redeploy** ## Summary * If you need variables during the build: use `ARG` \+ `ENV` in the Dockerfile * If your app only needs them when running: just use Railway's runtime injection (recommended) Make sure to click **Apply Changes** and **Redeploy** after updating variables in Railway.
10 months ago
Additional Context to my issue:
I have placed the appropriate variables into my Railway Variables and have redeployed. When checking my details, I can see the variables in the details tab.
loganbek
Do you no longer have the issue?
10 months ago
No, I still have the issue.
10 months ago
Here’s how I solved it on Railway—super simple:
- Use only runtime vars
I removed any
ARG/ENVorCOPY .env .lines from my Dockerfile. Railway injects the variables at run time, so my Dockerfile just becomes:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["gunicorn", "myproject.wsgi:application", "--bind", "0.0.0.0:8000"] - Match names exactly
In
settings.py, I read the vars exactly as I named them in Railway (case‑sensitive):
from decouple import config
STRIPE_PUBLIC_KEY = config('STRIPE_PUBLIC_KEY')
STRIPE_SECRET_KEY = config('STRIPE_SECRET_KEY') - Verify at runtime To be sure Railway applied them, I ran:
printenv | grep STRIPE and saw my keys printed out. If you don’t, just hit Apply Changes and Redeploy in Railway.
That’s it—no baking secrets into the image, no .env file, just Railway’s runtime vars and exact names.