Dockerized Django app - `gunicorn: command not found` on AWS deployment

I have a Dockerized Django app that is deployed on an AWS t3a.micro EC2 instance - after some recent updates to the Debian and PostgreSQL images that I'm using in the Dockerfile, the app is suddenly failing to start running successfully when it hits AWS due to an issue with gunicorn -

pyenv: gunicorn: command not found

This problem did not occur previously and, if I simply deploy a build based on the previous git commit (prior to making these updates), the app deploys and runs just fine. Furthermore, when I run the app locally via docker-compose up and bash into the container, I can run gunicorn commands no problem, so I'm not quite sure what the issue is - gunicorn is included in the requirements.txt file.

Maybe I'll need to provide more info, but for starters, here is the current setup of the Dockerfile and requirements.txt files - I'll note the changes that have been made below them -

Dockerfile

FROM andrejreznik/python-gdal:py3.11.10-gdal3.6.2
ENV PYTHONUNBUFFERED 1

RUN pip install --upgrade pip
RUN apt-get update 
RUN apt-get upgrade -y 

RUN apt-get install postgresql-15 postgresql-server-dev-15 -y

RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

ARG ALLOWED_HOSTS
ARG AWS_ACCESS_KEY_ID
ARG AWS_S3_CUSTOM_DOMAIN
ARG AWS_SECRET_ACCESS_KEY
ARG AWS_STORAGE_BUCKET_NAME
ARG AWS_SES_ACCESS_KEY_ID
ARG AWS_SES_SECRET_ACCESS_KEY
ARG CORS_ALLOWED_ORIGINS
ARG DATABASE_HOST
ARG DATABASE_NAME
ARG DATABASE_PASSWORD
ARG DATABASE_PORT
ARG DATABASE_USER
ARG SECRET_KEY

ENV ALLOWED_HOSTS=${ALLOWED_HOSTS}
ENV AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
ENV AWS_S3_CUSTOM_DOMAIN=${AWS_S3_CUSTOM_DOMAIN}
ENV AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
ENV AWS_STORAGE_BUCKET_NAME=${AWS_STORAGE_BUCKET_NAME}
ENV AWS_SES_ACCESS_KEY_ID=${AWS_SES_ACCESS_KEY_ID}
ENV AWS_SES_SECRET_ACCESS_KEY=${AWS_SES_SECRET_ACCESS_KEY}
ENV CORS_ALLOWED_ORIGINS=${CORS_ALLOWED_ORIGINS}
ENV DATABASE_HOST=${DATABASE_HOST}
ENV DATABASE_NAME=${DATABASE_NAME}
ENV DATABASE_PASSWORD=${DATABASE_PASSWORD}
ENV DATABASE_PORT=${DATABASE_PORT}
ENV DATABASE_USER=${DATABASE_USER}
ENV SECRET_KEY=${SECRET_KEY}

EXPOSE 8000

RUN ["python", "manage.py", "collectstatic", "--noinput"]
RUN ["python", "manage.py", "migrate"]

CMD ["gunicorn", "--workers=4", "--bind=:8000", "my_app_backend.wsgi"]

requirements.txt

boto3==1.16.18
django==3.1
django-ckeditor==6.0.0
django-cors-headers==3.5.0
django-filter==2.4.0
django-import-export>=2.5.0
django-ses==1.0.3
django-simple-history==2.11.0
django-storages==1.10.1
djangorestframework==3.11.1
djangorestframework-gis==0.16
dj-database-url==0.5.0
gunicorn==20.0.4
Pillow==7.2.0
psycopg2-binary==2.9.10
mysql-connector-python>=8.0.21
timezonefinder>=4.2.0
pytz>=2020.1

Between the last working release and this one, I've made the following changes to the Dockerfile -

  • updated Debian version from FROM andrejreznik/python-gdal:stable to FROM andrejreznik/python-gdal:py3.11.10-gdal3.6.2
  • added an explicit PostgreSQL version via RUN apt-get install postgresql-15 postgresql-server-dev-15 -y

And in requirements.txt -

  • updated psycopg2 dependency from psycopg2==2.8.5 to psycopg2-binary==2.9.10

Other than these changes, these files are exactly the same.

So far, I have experimented with various combinations of explicitly adding gunicorn to the Dockerfile via

RUN apt-get install gunicorn -y

and updating gunicorn in the requirements.txt to the latest version, but nothing has helped. For some reason, the gunicorn command is not becoming available in the deployed environment, but I don't understand why.

Please let me know if there is any other info I can provide that will help diagnose/resolve this problem.

Try to debug installation of gunicorn. In example add the following lines to your dockerfile and send the result log here

whereis gunicorn
which gunicorn
dpkg --get-selections | grep '^g'
pip list | grep '^g'
Back to Top