How to pip install in a docker image with a jenkins pipline step?

As I mentioned in this comment, the solution should be adding a proper user inside the container. Jenkins uses 984:984 for uid/gid on my machine (but may be different on yours - login to the host Jenkins is running on and execute sudo -u jenkins id -a to detect them), so you need to replicate it in the container that should be run by Jenkins:

FROM python:3.7

RUN mkdir /home/jenkins
RUN groupadd -g 984 jenkins
RUN useradd -r -u 984 -g jenkins -d /home/jenkins jenkins
RUN chown jenkins:jenkins /home/jenkins
USER jenkins
WORKDIR /home/jenkins

CMD ["/bin/bash"]

Of course, since you aren't the root user in the container anymore, either create a virtual environment:

$ docker run --rm -it jenkins/python /bin/bash
jenkins@d0dc87c39810:~$ python -m venv myenv
jenkins@d0dc87c39810:~$ source myenv/bin/activate
jenkins@d0dc87c39810:~$ pip install numpy

or use the --user argument:

$ docker run --rm -it jenkins/python /bin/bash
jenkins@d0dc87c39810:~$ pip install --user --upgrade pip
jenkins@d0dc87c39810:~$ pip install --user numpy

etc.


Alternatively, you can (but in most cases shouldn't) enter the container as root, but with jenkins group:

$ docker run --user 0:984 ...

This way, although the modified files will still change the owner, their group ownership will still be intact, so Jenkins will be able to clean up the files (or you can do it yourself, via

sh 'rm -f modified_file'

in the Jenkinsfile.