Django with gunicorn: server error, Application failed to respond

gasullTRIAL

a year ago

I can build and deploy my application correctly, but the application doesn't respond:

https://goat-book-staging.up.railway.app/

I get a server error in the browser with the message "Application failed to respond".

Here's my Dockerfile:

FROM python:slim

RUN python -m venv /venv
ENV PATH="/venv/bin:$PATH"

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

COPY src /src

WORKDIR /src

ENV DJANGO_DEBUG_FALSE=1

# Read Railway environment variables
ENV DJANGO_SECRET_KEY=$DJANGO_SECRET_KEY
ENV DJANGO_ALLOWED_HOST=$DJANGO_ALLOWED_HOST

RUN python manage.py collectstatic

EXPOSE 8888
CMD gunicorn --bind 0.0.0.0:80 superlists.wsgi:application

11 Replies

a year ago

Change the CMD line to -

CMD ["gunicorn", "superlists.wsgi:application"]

And remove the EXPOSE line.


gasullTRIAL

a year ago

Thank you for your response, but I still get the "Application failed to respond" error after applying your changes.


a year ago

Do you have a start command set elsewhere? service settings, Procfile, railway.json, etc. If so, remove it from anywhere that isn't that CMD line in your Dockerfile.


gasullTRIAL

a year ago

No, I don't have anything besides what I shared in the Dockerfile.

Maybe I use someone's Railway template for deployment with Django and add my code to it. Does such Railway template exist?


a year ago

Yes, there is a django template, but that really should not be necessary.

Please attach your deployment logs -

https://bookmarklets.up.railway.app/log-downloader/

And attach your new Dockerfile.


gasullTRIAL

a year ago

Here are the build logs, the deploy logs and the Dockerfile. Thanks.


a year ago

Going forward, please only ever use the bookmarklet to send your logs.

This is not needed and is bound to do more harm than good, please remove it, not to mention it's done incorrectly too.

# Read Railway environment variables
ENV DJANGO_SECRET_KEY=$DJANGO_SECRET_KEY
ENV DJANGO_ALLOWED_HOST=$DJANGO_ALLOWED_HOST

Tell me more about what your app does at first boot please, you may be running into the memory limits of the trial plan and causing your app to be killed.


gasullTRIAL

a year ago

Going forward, please only ever use the bookmarklet to send your logs.

Do you mean this?

https://bookmarklets.up.railway.app/log-downloader/

This is not needed and is bound to do more harm than good, please remove it, not to mention it's done incorrectly too.

I have this code in the settings.py of my Django project:

if "DJANGO_DEBUG_FALSE" in os.environ:
    DEBUG = False
    SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
    ALLOWED_HOSTS = [os.environ["DJANGO_ALLOWED_HOST"]]

I was running into other errors because settings.py could not find the variables I defined in "Variables" on the Railway.app UI. After deleting the lines in Dockerfile, I get the errors again, as you can see in the new logs attached.

Tell me more about what your app does at first boot please, you may be running into the memory limits of the trial plan and causing your app to be killed.

It's a very simple to-do list app that it's only half done as I'm reading the book Test-Driven Development with Python (https://www.obeythetestinggoat.com/). It shouldn't use much memory.

Attachments


a year ago

Looks like "python manage.py collectstatic" does need those variables, my bad I had not released something that collects static files would need those variables, and in that case, you'll need to add them back, but correctly -

# Read Railway environment variables
ARG DJANGO_SECRET_KEY
ARG DJANGO_ALLOWED_HOST

As for my idea of being out of memory -

[2024-04-22 02:50:51 +0000] [8] [INFO] Booting worker with pid: 8
[2024-04-22 03:06:49 +0000] [7] [INFO] Handling signal: term

Those events are not super close, So this seems more like the term signal that gets sent to the old deployment when a new deployment goes out. If so, nothing out of the ordinary there.

Can you make those ARG changes to your Dockerfile and then attach your latest deployment logs?


gasullTRIAL

a year ago

This fixed it! Thank you so much.


a year ago

Awesome, it indeed did more harm how you had those environment variables.