Recommended way to install pip packages inside Dockerfile

Hello, I’m learning programming and I’m writing first Python application that does something useful, and practical. What is the recommended way to install pip packages when building Docker images? I’m asking because pip install complains about running itself as root. Do you recommend to create venv in Dockerfile? Thank you.

running pip install as root is not recommended in general as far as I know, because it changes system python packages and pip usually recommends using the OS package manager like apt to install python3-PKGNAME if available when the package needed system-wide. I’m not sure how that is better, I just saw that recommendation. I think it is because some packages are already installed through the OS package manager and in that case installing the rest that way is preferred instead of having another collection system-wide python packages.
You can also use virtual environment regardless of Docker.
You can use venv, virtualenv or I often hear about “uv”