0

I have this Dockerfile:

FROM python:3.8-slim

WORKDIR /app

COPY . .

RUN apt-get update
RUN apt-get install -y python3 python3-pip python3-venv

RUN pip freeze > requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

CMD ["python3", "main.py"]

Everything works file until this line:

RUN pip install --no-cache-dir -r requirements.txt

Using docker run --rm -it name bash and pip install -r requirements.txt then I found this error:

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

Here, I found solution (which didn't work for me), that it's possible to resolve just by creating new user, but it doesn't seem to be optimal solution. How can I fix this?

dokichan
  • 857
  • 2
  • 11
  • 44
  • _but it doesn't seem to be optimal solution_ > on what are you basing this statement? – β.εηοιτ.βε Apr 16 '22 at 14:23
  • I mean, it seems to be a little bit weird - create user, just to give him permissions to execute script, but still, didn't work for me. – dokichan Apr 16 '22 at 14:25
  • Well, that's the way though. The warning of pip point you at that _you should ideally not run pip as root_. If you are not willing to comply with the warning, nothing forces you, though, so, just ignore it, or use a virtual environment, as pointed by the warning. – β.εηοιτ.βε Apr 16 '22 at 14:27
  • And if the said answer did not work for you, then please be more precise **what** did not work for you? – β.εηοιτ.βε Apr 16 '22 at 14:28
  • Also worth reading: https://stackoverflow.com/questions/68155641/should-i-run-things-inside-a-docker-container-as-non-root-for-safety – β.εηοιτ.βε Apr 16 '22 at 14:30
  • In that solution this line - `ENV PATH="/home/myuser/.local/bin:$PATH"` - didn't work for me. – dokichan Apr 16 '22 at 14:44

1 Answers1

0

In this case the problem was in version of images. Using this Dockerfile I was able to fix this:

FROM python:3.9.3

WORKDIR /app

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

COPY . .

CMD ["python3", "main.py"]

PS. I don't really know if it was about it, but this images has the same python version, that I have on my computer. I could have impact on dependencies.

dokichan
  • 857
  • 2
  • 11
  • 44
  • 1
    you probably still run pip as root user. the thing is in a docker container, it doesnt matter too much, because you install only this one set of depenecies and dont have multiple packages and so on. I think its still better practice to use a venv. – The Fool Apr 16 '22 at 18:41