Flask/Gunicorn app fails to start on Railway: '$PORT' is not a valid port number
istemihan90
HOBBYOP

8 months ago

My Python Flask application, served with Gunicorn, is failing to start on Railway. The deployment process completes, but the container repeatedly crashes with an error indicating that the $PORT environment variable is not being correctly interpreted as a valid port number by Gunicorn. This prevents the application from binding to the assigned port.

The primary error message observed in the Railway logs is: Error: '$PORT' is not a valid port number.

Logs (build and/or deploy):

Starting Container
 
Error: '$PORT' is not a valid port number.
 
Error: '$PORT' is not a valid port number.
... (repeats)

Link to GitHub repohttps://github.com/istemihan90/unity-builder-service)

Dockerfile content:

# game-ci/unity-builder imajını temel al
# 'not found' hatasını çözmek için SHA256 digest'i ile doğrudan referans verildi.
FROM istemihan90/unityci-editor@sha256:4213678926b1b7387c12d84628d866b75f9d911c87d39979f5aff0cf8d7fe210

# Çalışma dizinini ayarla
WORKDIR /app

# pip'i ve bash'i yüklemek için gerekli sistem bağımlılıklarını ve pip'i kur
# Unity imajı Ubuntu tabanlı olduğu için apt kullanılır
RUN apt-get update && apt-get install -y python3-pip bash

# Uygulama bağımlılıklarını kopyala ve yükle
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Uygulama kodunu kopyala
COPY . .

# Flask uygulamasını çalıştırmak için komutu tanımla
# $PORT ortam değişkeninin doğru şekilde genişletilmesi için shell formu kullanıldı.
CMD gunicorn --bind 0.0.0.0:$PORT app:app --timeout 600

Steps taken to troubleshoot: I have attempted several solutions based on common Docker and Gunicorn $PORT issues, including:

  1. Using the exec form of CMD with /bin/sh -c (e.g., CMD ["/bin/sh", "-c", "gunicorn ..."]).

  2. Switching to /bin/bash -c in the exec form (e.g., CMD ["/bin/bash", "-c", "gunicorn ..."]).

  3. Installing bash explicitly within the Dockerfile (RUN apt-get install -y bash).

  4. Using the ${PORT} syntax for environment variable expansion (e.g., CMD ["/bin/bash", "-c", "gunicorn --bind 0.0.0.0:${PORT} ..."]).

  5. Using the exec command within the shell form (e.g., CMD ["/bin/bash", "-c", "exec gunicorn ..."]).

  6. Currently, I am using the direct shell form CMD gunicorn --bind 0.0.0.0:$PORT app:app --timeout 600, which was suggested in a similar Reddit thread for Railway deployments.

Despite these attempts, the error persists. It seems the $PORT variable is still not being correctly resolved to a numeric value when Gunicorn starts.

Any guidance or suggestions would be greatly appreciated!

Solved

1 Replies

istemihan90
HOBBYOP

8 months ago

It is solved after I removed the start command of the container in settings.


Status changed to Solved chandrika 8 months ago


Loading...