Home > Back-end >  gunicorn: command not found
gunicorn: command not found

Time:07-30

I get gunicorn: command not found when I run my Docker image. And when I shell into image and run it manually I get the same error.

However, the output of pip install gunicorn confirms it is installed:

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: gunicorn in ./.local/lib/python3.10/site-packages (20.1.0)
Requirement already satisfied: setuptools>=3.0 in /usr/local/lib/python3.10/site-packages (from gunicorn) (58.1.0)

Here are the files to build this image:

Dockerfile

FROM python:3.10.5-alpine

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

RUN adduser -D appuser

USER appuser

WORKDIR /home/appuser/

COPY --chown=appuser:appuser requirements.txt .

RUN python -m pip install --user --no-cache-dir --disable-pip-version-check --requirement requirements.txt

COPY --chown=appuser:appuser . .

ENTRYPOINT [ "./entrypoint.sh" ]

requirements.txt

django==4.0.6
psycopg2-binary==2.9.3
gunicorn==20.1.0

entrypoint.sh

#!/bin/sh
python manage.py makemigrations
python manage.py migrate
python manage.py collectstatic --no-input
gunicorn project.wsgi:application --bind=0.0.0.0:8000 --workers=4 --timeout=300 --log-level=debug --log-file=-

CodePudding user response:

You should RUN pip install as root, without the --user option.

If you run pip install --user, it installs packages into ~/.local. In Docker the $HOME environment variable isn't usually defined, so this will install them into /.local. If you looked inside the built image, you'd probably find a runnable /.local/bin/gunicorn, but that /.local/bin directory isn't on the $PATH.

Typical use in Docker is to install Python packages into the "system" Python; the Docker image itself is isolation from other Pythons. This will put the *.py files into somewhere like /usr/local/lib/python3.10/site-packages, and the gunicorn executable will be in /usr/local/bin, which is on the standard $PATH.

FROM python:3.10.5-alpine
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
RUN adduser -D appuser
WORKDIR /home/appuser/
# but do not change USER yet

COPY --chown=appuser:appuser requirements.txt .

# Without --user option
RUN python -m pip install --no-cache-dir --disable-pip-version-check --requirement requirements.txt

# Change USER _after_ RUN pip install
USER appuser

COPY --chown=appuser:appuser . .
ENTRYPOINT [ "./entrypoint.sh" ]
  • Related