R
Railwayβ€’15mo ago
zimapurple

Having trouble starting gunicorn with Django deployed from dockerfile resulting in 503

Trying to deploy a django project that was developed locally with this Docker File
# Pull base image
FROM python:3.10.4-slim-bullseye

# Set environment variables
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Install dependencies
COPY ./requirements.txt .
RUN pip install -r requirements.txt

# Copy project
COPY . .
# Pull base image
FROM python:3.10.4-slim-bullseye

# Set environment variables
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Install dependencies
COPY ./requirements.txt .
RUN pip install -r requirements.txt

# Copy project
COPY . .
From the Django Railway template I've set the railway.json to be:
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS"
},
"deploy": {
"startCommand": "python manage.py migrate && gunicorn engi_alpha.wsgi",
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 1
}
}
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS"
},
"deploy": {
"startCommand": "python manage.py migrate && gunicorn engi_alpha.wsgi",
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 1
}
}
I've also set ALLOWED_HOSTS to be *, though I would like to make that specific soon. After running railway up in a linked project the build appears to install all requirements fine, and proceeds to deploy. With a Postgres DB service I'm able to get the DB to run the migrations successfully, creating empty tables, however at that point the logs stop (no errors, just no more entries). I've attached a screenshot of the repo structure and the end of the railway up command. I feel like I'm missing something to do with either permissions, URLS or the deployment commands, but I've not got there yet. It's my first personal project in web dev so I;m sure the answer is obvious to many :p Any help appreciated πŸ™‚
36 Replies
Percy
Percyβ€’15mo ago
Project ID: ee6fe531-fb8f-45f3-906e-f22374d487ac
zimapurple
zimapurpleβ€’15mo ago
ee6fe531-fb8f-45f3-906e-f22374d487ac
Brody
Brodyβ€’15mo ago
does the logs in the railway UI also stop at this point?
zimapurple
zimapurpleβ€’15mo ago
They do
Brody
Brodyβ€’15mo ago
looks like the migration runs, but it never gets to start gunicorn do you have a start setting configured somewhere else like in the service settings?
zimapurple
zimapurpleβ€’15mo ago
If you mean through the value in permissable-voleyball > settings > deploy > start command the value has a nite that say it's sourced from the settings file. Was that the question? Also, Ive just come back to the page and am now seeing Deployments Paused Builds need to drain. null
Brody
Brodyβ€’15mo ago
yeah lets come back to this after the incidents are resolved https://discord.com/channels/713503345364697088/846875565357006878/1103346301829136414
zimapurple
zimapurpleβ€’15mo ago
Sure, thanks for taking a gander
Brody
Brodyβ€’15mo ago
hey wanna try again?
zimapurple
zimapurpleβ€’15mo ago
Sure, I'll run a 'railway up' and see what happens.
zimapurple
zimapurpleβ€’15mo ago
Interesting, totally different error. Trying to work out if I changed something that would cause it. Looks like the behaviour is now to prioritise the settings file and not the docker file?
Brody
Brodyβ€’15mo ago
click the code button and show me a screenshot
zimapurple
zimapurpleβ€’15mo ago
except it's also not accessing my settings file? locally it;s still got my start command
zimapurple
zimapurpleβ€’15mo ago
Brody
Brodyβ€’15mo ago
you must not have your railway.json file in the right place
zimapurple
zimapurpleβ€’15mo ago
ohhhhh i;ve run the cli in the wrong place, was working in a diff project. 1 sec
Brody
Brodyβ€’15mo ago
show me your dockerignore file lol
zimapurple
zimapurpleβ€’15mo ago
XD thought it was odd I had to relink the project.... building (theres a fat dep tree here 😦 )
Brody
Brodyβ€’15mo ago
whats your requirements.txt look like?
zimapurple
zimapurpleβ€’15mo ago
OK, build complete, succesful. Depployment completed, logs stop at no migrations to apply
asgiref==3.6.0
Bokeh == 3.1.0
crispy-bootstrap5==0.6
Django==4.0.10
django-allauth==0.50.0
django-crispy-forms==2.0
djlint==1.23.0
environs[django]==9.5.0
gunicorn==20.1.0
pandas==1.5.3
pandera==0.13.4
pytest-django==4.5.2
psycopg2-binary==2.9.3
requests==2.28.2
requests-mock==1.9.3
statsforecast==1.5.0
sqlparse==0.4.3
types-requests==2.28.11.17
whitenoise==6.2.0
asgiref==3.6.0
Bokeh == 3.1.0
crispy-bootstrap5==0.6
Django==4.0.10
django-allauth==0.50.0
django-crispy-forms==2.0
djlint==1.23.0
environs[django]==9.5.0
gunicorn==20.1.0
pandas==1.5.3
pandera==0.13.4
pytest-django==4.5.2
psycopg2-binary==2.9.3
requests==2.28.2
requests-mock==1.9.3
statsforecast==1.5.0
sqlparse==0.4.3
types-requests==2.28.11.17
whitenoise==6.2.0
statsforcast has a fat set of it;s own dependencies however, they all completed
Brody
Brodyβ€’15mo ago
did build finish?
zimapurple
zimapurpleβ€’15mo ago
yup yup same behaviour
zimapurple
zimapurpleβ€’15mo ago
Is it possible the difference between my railway.json and the fact I have a dockerfile in the repo itself is causing issues? The deploy is obviously automatically pointing at my docker, event though the railway.json points it to "NIXPACKS"
Brody
Brodyβ€’15mo ago
why not just remove the railway.json file and then do (hold on)
# Pull base image
FROM python:3.10.4-slim-bullseye

# Set environment variables
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Copy project
COPY . .

# Install dependencies
RUN pip install -r requirements.txt

# run migrations
RUN python manage.py migrate

# start server
CMD gunicorn engi_alpha.wsgi
# Pull base image
FROM python:3.10.4-slim-bullseye

# Set environment variables
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Copy project
COPY . .

# Install dependencies
RUN pip install -r requirements.txt

# run migrations
RUN python manage.py migrate

# start server
CMD gunicorn engi_alpha.wsgi
so yeah delete the railway.json file, and ust that as your dockerfile
zimapurple
zimapurpleβ€’15mo ago
I'll have a crack
zimapurple
zimapurpleβ€’15mo ago
Failed at RUN python manage.py migrate, e.g. the step before the logs dropped previously. Issue seems to be related to detecting the SECRET_KEY varable, which it must have found succesfully before and is set in teh env vars
Brody
Brodyβ€’15mo ago
# Pull base image
FROM python:3.10.4-slim-bullseye

# Set environment variables
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Copy project
COPY . .

# Install dependencies
RUN pip install -r requirements.txt

# run migrations and start server
CMD python manage.py migrate && gunicorn engi_alpha.wsgi
# Pull base image
FROM python:3.10.4-slim-bullseye

# Set environment variables
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Copy project
COPY . .

# Install dependencies
RUN pip install -r requirements.txt

# run migrations and start server
CMD python manage.py migrate && gunicorn engi_alpha.wsgi
try that
zimapurple
zimapurpleβ€’15mo ago
well that built a lot faster, I guess railway caches the layer?
Brody
Brodyβ€’15mo ago
maybe
zimapurple
zimapurpleβ€’15mo ago
OHO! Closer, got more logs. URL still errors, but 301 this time.
Brody
Brodyβ€’15mo ago
okay and now its a code issue, that i unfortunately cant help with, off to google you go!
zimapurple
zimapurpleβ€’15mo ago
Sure, thanks for your help this evening, I'll head into the wild tomorrow.
Brody
Brodyβ€’15mo ago
no problem!
zimapurple
zimapurpleβ€’15mo ago
Seemingly just solved using the same info as in this SO: https://stackoverflow.com/questions/55726610/django-ssl-redirection-on-heroku-too-many-redirects Specifically adding the line SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') to the settings.py file. My current (and new) understanding is that this is a required setting when your project starts implementing more secure practices on proxy servers?
Stack Overflow
Django SSL redirection on Heroku: 'Too many redirects'
I have a web app deployed to Heroku with a custom domain name which DNS is managed through CloudFlare. What I want to do is redirect HTTP requests to HTTPS. After setting SECURE_SSL_REDIRECT to T...
zimapurple
zimapurpleβ€’15mo ago
@Brody I've spotted that railway has community templates, and there is one for Django, however I didn't start from there because a) I wasn't sure if this would ever be worth deploying, b) the template used is extremely basic, verging on unsafe unless configured? and c) I didn't actually know about railway in the first stage of the work. Therefore my question is effectively: Is this deliberately railways approach, e.g. ALL complexity of the framework is up to the user, or is there an interest in 1) extending the existing template or 2) creating a second "auth focussed" django template? (fine both ways btw, straight up honest question)
Brody
Brodyβ€’15mo ago
the purpose of the framework templates (like django's template) is just to show the user how to get the project active on railway, the rest it completely up to the user