Home > Blockchain >  Default pip package PATH in python:3.8-slim-buster docker image
Default pip package PATH in python:3.8-slim-buster docker image

Time:06-29

Im installing gunicorn pip package in my docker python:3.8-slim-buster image and when I use CMD gunicorn im told /bin/sh: 1: gunicorn: not found.

So im considering changing the path but i have a few questions to do so :

should i use (in my Dockerfile):

pip --target=path_already_in_PATH install gunicorn
ENV PYTHONPATH "${PYTHONPATH}:good_path"
ENV PATH="/default_pip_path:${PATH}"

I dont know which option is better and what to put in good_path, path_already_in_PATH and default_pip_path

This is my Dockerfile :

FROM python:3.8-slim-buster
RUN apt-get update \
  # dependencies for building Python packages
  && apt-get install -y build-essential netcat
  # cleaning up unused files
#  && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
#  && rm -rf /var/lib/apt/lists/*
RUN addgroup --system kr1p \
      && adduser --system --ingroup kr1p kr1p
WORKDIR /app
COPY app .
RUN chown -R kr1p:kr1p /app

USER kr1p
RUN pip install -r requirements.txt 
ENV PYTHONUNBUFFERED 1
CMD gunicorn

I ve also tried python -m gunicorn but it's the same and also CMD ["gunicorn"]

And the docker-compose.yml

---
version: '3.7'

services:
  app:
    container_name: app
    build:
      context: .
      dockerfile: ./app/Dockerfile
    volumes:
      - app:/app
    ports:
      - 5000:5000

volumes:
  app:
    name: app

I noticed pip says "Defaulting to user installation because normal site-packages is not writeable" at the begining of the installation probably because i've created a new user

It's another issue but pip also tells me at the end : #10 385.5 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

What is the proper way to set a virtualenv to avoid issues?

CodePudding user response:

Ah, so the problem shows up in the docker build output:

Step 8/10 : RUN pip install gunicorn
 ---> Running in 5ec725d1c957
Defaulting to user installation because normal site-packages is not writeable
Collecting gunicorn
  Downloading gunicorn-20.1.0-py3-none-any.whl (79 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 79.5/79.5 KB 2.3 MB/s eta 0:00:00
Requirement already satisfied: setuptools>=3.0 in /usr/local/lib/python3.8/site-packages (from gunicorn) (57.5.0)
Installing collected packages: gunicorn
  WARNING: The script gunicorn is installed in '/home/kr1p/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed gunicorn-20.1.0
WARNING: You are using pip version 22.0.4; however, version 22.1.2 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Removing intermediate container 5ec725d1c957
 ---> c42800562d88
Step 9/10 : ENV PYTHONUNBUFFERED 1
 ---> Running in 8d9342ec2288```

Namely: " WARNING: The script gunicorn is installed in '/home/kr1p/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location."

This is because it's running as your non-root kr1p user, so it's actually ending up in $HOME/.local/bin/gunicorn instead.

I would either:

add that dir to the PATH statically in the dockerfile, like:

ENV PATH=/home/kr1p/.local/bin:$PATH

or, install dependencies as root, prior to switching down to the unpriv user for copying source files and other setup.

USER root
COPY requirements.txt /reqs.txt
RUN pip install --root-user-action=ignore -r /reqs.txt
USER kr1p
COPY --chown kr1p app/ ./

The root-user-action is just to suppress a message about how you should be using virtualenvs, which doesn't necessarily apply when walling things off inside a container instead. This requires a newer pip than that which comes with debian-buster though, so I ended up removing it (and you're just stuck with that warning if you use the install while root approach).

As a full working example for the PATH modifying approach, see:

FROM python:3.8-slim-buster
RUN apt-get update \
  # dependencies for building Python packages
  && apt-get install -y build-essential netcat
  # cleaning up unused files
#  && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
#  && rm -rf /var/lib/apt/lists/*

# sets kr1p home dir to /app
RUN adduser --home /app --system --group kr1p

ENV PATH=/app/.local/bin:$PATH \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1

WORKDIR /app
COPY app/requirements.txt .

USER kr1p
RUN pip install -r /app/requirements.txt
COPY --chown=kr1p:kr1p app .

# otherwise a shell runs gunicorn, and signals don't get passed down properly
CMD ["gunicorn", "--help"]

(There were a few other things wrong like a missing = in your ENV statement, etc.)

  • Related