a year ago
Details:
Environment: I’m using a Python 3.12-slim Docker image.
Application: Django backend served with Uvicorn.
Deployment: The app builds successfully, and migrations run without issues. However, when accessing the app, it returns a 502 error.
Port Configuration: The
PORTvariable doesn’t seem to be correctly interpreted within the Docker container. The error logs show that"$PORT"is not being recognized as a valid integer, causing Uvicorn to fail to start.
Steps Taken:
Dockerfile Configuration:
I used the recommended Railway Dockerfile setup with Uvicorn, ensuring all necessary dependencies are installed (libpq-dev, gcc, libmariadb-dev, etc.).
I’ve tried both setting the
PORTdirectly and using it as an environment variable, but Uvicorn still fails to recognize it.
The app runs but throws a 502 error when accessed, likely due to the incorrect handling of the PORT variable. I need help identifying why the PORT variable isn’t being correctly recognized and ensuring the app runs on the expected port.
Dockerfile:
FROM python:3.12-slim
WORKDIR /app
RUN apt-get update && apt-get install -y \ libpq-dev \ gcc \ libmariadb-dev-compat \ libmariadb-dev \ pkg-config \ && apt-get clean
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD uvicorn main:app --host 0.0.0.0 --port $PORT
Url: https://eucerinv2backend-production.up.railway.app/
3 Replies
a year ago
Hi, could you echo $PORT on start, in order to check what's being passed ?
I recommend testing your dockerfile locally to try and reproduce the error.
Please note that you can also just define PORT in your service's env vars, and use a fixed port on boot.
Example: CMD uvicorn main:app --host 0.0.0.0 --port 3200 and PORT=3200 in your service's env.
Docs for this can be found here: https://docs.railway.app/guides/public-networking#port-variable
Status changed to Awaiting User Response Railway • over 1 year ago
a year ago
solved!
changed to: CMD uvicorn eucerinescuela_backend.asgi:application --host 0.0.0.0 --port 8080
Status changed to Awaiting Railway Response Railway • over 1 year ago
Status changed to Awaiting User Response Railway • over 1 year ago
Status changed to Solved itsrems • over 1 year ago
