R
Railway4mo ago
Nahasco

container event container died when trying to deploy with dockerfile

I needed to use a dockerfile to install some important dependencies but now the deployment is failing on me with "container event container died" message in the deploy logs.
FROM python:3.12.2-alpine

WORKDIR /usr/src/app

ENV PYTHONUNBUFFERED=1

RUN pip install --upgrade pip

RUN apk add --update --no-cache --virtual .tmp-build-deps \
gcc libc-dev linux-headers postgresql-dev \
&& apk add libffi-dev \
&& apk add tk

RUN apk add weasyprint

RUN apk --update --upgrade --no-cache add fontconfig ttf-freefont font-noto terminus-font \
&& fc-cache -f \
&& fc-list | sort

COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

COPY . /usr/src/app/
FROM python:3.12.2-alpine

WORKDIR /usr/src/app

ENV PYTHONUNBUFFERED=1

RUN pip install --upgrade pip

RUN apk add --update --no-cache --virtual .tmp-build-deps \
gcc libc-dev linux-headers postgresql-dev \
&& apk add libffi-dev \
&& apk add tk

RUN apk add weasyprint

RUN apk --update --upgrade --no-cache add fontconfig ttf-freefont font-noto terminus-font \
&& fc-cache -f \
&& fc-list | sort

COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

COPY . /usr/src/app/
this is my dockerfile. attached are the logs.
Solution:
After testing the response is successful however it takes 33 seconds and it seems like the pdf styling wasnt loading fixed it by increasing gunicorn workers to 8 and running collectstatic...
Jump to solution
36 Replies
Percy
Percy4mo ago
Project ID: 643c9962-5fa0-449c-82e0-67b30f66c552
Nahasco
NahascoOP4mo ago
643c9962-5fa0-449c-82e0-67b30f66c552 ok it seems like i never run the application so i changed the docker file to this
# Use an official Python runtime as a parent image
FROM python:3.12.2-alpine

# Set the working directory in the container
WORKDIR /usr/src/app

# Ensure that the Python output is not buffered (helpful for Docker logs)
ENV PYTHONUNBUFFERED=1

# Upgrade pip
RUN pip install --upgrade pip

# Install system dependencies
RUN apk add --update --no-cache --virtual .tmp-build-deps \
gcc libc-dev linux-headers postgresql-dev \
&& apk add libffi-dev \
&& apk add tk

# Install WeasyPrint and its dependencies
RUN apk add weasyprint

# Install fonts
RUN apk --update --upgrade --no-cache add fontconfig ttf-freefont font-noto terminus-font \
&& fc-cache -f \
&& fc-list | sort

# Install Python dependencies
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

# Copy the rest of the application code
COPY . /usr/src/app/

# Collect static files
RUN python manage.py collectstatic --noinput

# Run database migrations
RUN python manage.py migrate

# Expose the port that the app runs on
EXPOSE 8000

# Start the application
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "tibian_backend.wsgi:application"]
# Use an official Python runtime as a parent image
FROM python:3.12.2-alpine

# Set the working directory in the container
WORKDIR /usr/src/app

# Ensure that the Python output is not buffered (helpful for Docker logs)
ENV PYTHONUNBUFFERED=1

# Upgrade pip
RUN pip install --upgrade pip

# Install system dependencies
RUN apk add --update --no-cache --virtual .tmp-build-deps \
gcc libc-dev linux-headers postgresql-dev \
&& apk add libffi-dev \
&& apk add tk

# Install WeasyPrint and its dependencies
RUN apk add weasyprint

# Install fonts
RUN apk --update --upgrade --no-cache add fontconfig ttf-freefont font-noto terminus-font \
&& fc-cache -f \
&& fc-list | sort

# Install Python dependencies
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

# Copy the rest of the application code
COPY . /usr/src/app/

# Collect static files
RUN python manage.py collectstatic --noinput

# Run database migrations
RUN python manage.py migrate

# Expose the port that the app runs on
EXPOSE 8000

# Start the application
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "tibian_backend.wsgi:application"]
but i got errors in collectstatic and migrate commands i think the env vars arent present when building
Nahasco
NahascoOP4mo ago
tried this but getting container died again
Nahasco
NahascoOP4mo ago
can i get some help please
Brody
Brody4mo ago
Nahasco
NahascoOP4mo ago
thank but i was able to finally deploy successfully when i removed a custom start command i put in previously
Brody
Brody4mo ago
glad to hear that!
Nahasco
NahascoOP4mo ago
i have another question though, i am trying to generate a pdf in django and then send it to frontend. everyhting works fine in development but in prod i get these errors
from origin "" has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

net::ERR_FAILED 503 (Service Unavailable)
from origin "" has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

net::ERR_FAILED 503 (Service Unavailable)
Brody
Brody4mo ago
what do you get when you simply open the backend's domain in your browser?
Nahasco
NahascoOP4mo ago
does this have to do with railway limitations or something with the prod setup i will try that
Brody
Brody4mo ago
I'm confident enough to say that this would be a misconfiguration, I'm sure we can get to the cause
Nahasco
NahascoOP4mo ago
oh i remembered the request should be post with auth headers, so that wont be possible GET /api/reports/session/59/ HTTP 401 Unauthorized Allow: POST, OPTIONS Content-Type: application/json Vary: Accept WWW-Authenticate: Bearer realm="api" { "type": "client_error", "errors": [ { "code": "not_authenticated", "detail": "Authentication credentials were not provided.", "attr": null } ] } should i try to bypass that for testing?
Brody
Brody4mo ago
is that what you get when you open the site in your browser?
Nahasco
NahascoOP4mo ago
yes
Brody
Brody4mo ago
okay that's a good sign, as long as it's not a railway error page that's all that matters looks like you will need to use some cors middleware
Nahasco
NahascoOP4mo ago
i do already and the rest of my app works fine, only this mew route that returns a pdf isnt working
Brody
Brody4mo ago
maybe it's not really a cors error and instead it's returning something like a 500 status code that doesn't have cors headers on it, you'd want to test with something like postman that doesn't care about cors to see what's actually happening
Nahasco
NahascoOP4mo ago
Tried that and got the railway failed to respond error, 503 service unavailable
Brody
Brody4mo ago
ah yes just as I suspected, your app did not respond you will have to look to your deployment logs for errors on why it didn't respond
Nahasco
NahascoOP4mo ago
[2024-07-29 11:24:46 +0000] [79] [CRITICAL] WORKER TIMEOUT (pid:262) [2024-07-29 11:24:46 +0000] [262] [INFO] Worker exiting (pid: 262) [2024-07-29 11:24:47 +0000] [79] [ERROR] Worker (pid:262) exited with code 1 [2024-07-29 11:24:47 +0000] [79] [ERROR] Worker (pid:262) exited with code 1. [2024-07-29 11:24:47 +0000] [343] [INFO] Booting worker with pid: 343 [2024-07-29 11:30:12 +0000] [79] [CRITICAL] WORKER TIMEOUT (pid:343) [2024-07-29 11:30:13 +0000] [343] [INFO] Worker exiting (pid: 343) [2024-07-29 11:30:13 +0000] [79] [ERROR] Worker (pid:343) exited with code 1 [2024-07-29 11:30:13 +0000] [79] [ERROR] Worker (pid:343) exited with code 1. [2024-07-29 11:30:13 +0000] [424] [INFO] Booting worker with pid: 424 [2024-07-29 12:09:59 +0000] [79] [CRITICAL] WORKER TIMEOUT (pid:424) [2024-07-29 12:09:59 +0000] [424] [INFO] Worker exiting (pid: 424) [2024-07-29 12:10:00 +0000] [79] [ERROR] Worker (pid:424) exited with code 1 [2024-07-29 12:10:00 +0000] [79] [ERROR] Worker (pid:424) exited with code 1. [2024-07-29 12:10:00 +0000] [533] [INFO] Booting worker with pid: 533 worker timed out? is that the issue?
Brody
Brody4mo ago
that means a particular request took over the default of 30 seconds and was stopped, I assume generating a pdf should never take anywhere near 30 seconds?
Nahasco
NahascoOP4mo ago
anyway to increase default value?
Brody
Brody4mo ago
you only need to if pdf generation is supposed to take longer than 30 seconds
Nahasco
NahascoOP4mo ago
I do alot of querysets to fetch the data for the pdf, so maybe its normal for it to take over 30 seconds, i am not sure so i want to test if increasing the value will fix the error, if it does then ill work on optimizing the process
Brody
Brody4mo ago
fair enough, what's your current start command
Nahasco
NahascoOP4mo ago
i use a dockerfile
Brody
Brody4mo ago
what is the start command in that bash file
Nahasco
NahascoOP4mo ago
its defined above it gunicorn ${PROJ_NAME}.wsgi:application --bind "0.0.0.0:$RUN_PORT" i define the script in the same place, there is no external bash file what do i do with it?
Brody
Brody4mo ago
you would want to set this flag - https://docs.gunicorn.org/en/stable/settings.html#timeout to something greater than 30
Solution
Nahasco
Nahasco4mo ago
After testing the response is successful however it takes 33 seconds and it seems like the pdf styling wasnt loading fixed it by increasing gunicorn workers to 8 and running collectstatic
Nahasco
NahascoOP4mo ago
any downside to settings workers to 8? do i still need to optimise my querying?
Brody
Brody4mo ago
how long does the request to generate a pdf take now?
Nahasco
NahascoOP4mo ago
a second or so
Brody
Brody4mo ago
okay then id say thats pretty good
Nahasco
NahascoOP4mo ago
Great. You've been incredibly helpful! Thank you sincerely.
Brody
Brody4mo ago
no problem!
Want results from more Discord servers?
Add your server