Jenkins in a Docker Container - How do I install custom Python libraries?

Viewed 9009

So, after building out a pipeline, I realized I will need some custom libraries for a python script I will be pulling from SCM. To install Jenkins in Docker, I used the following tutorial:

https://jenkins.io/doc/book/installing/

Like so:

docker run \
  -u root \
  --rm \  
  -d \ 
  -p 8080:8080 \ 
  -p 50000:50000 \ 
  -v jenkins-data:/var/jenkins_home \ 
  -v /var/run/docker.sock:/var/run/docker.sock \ 
  jenkinsci/blueocean 

Now, I will say I'm not a Docker guru, but I'm aware the Dockerfile allows for passing in library installs for Python. However, because I'm pulling the docker image from dockerhub, I'm not sure if it's possible to add a "RUN pip install " as an argument. Maybe there is an alternate approach someone may have.

Any help is appreciated.

EDIT 1: Here's the output of the first commenter's recommendation:

Step 1/6 : FROM jenkinsci/blueocean
 ---> b7eef16a711e
Step 2/6 : USER root
 ---> Running in 150bba5c4994
Removing intermediate container 150bba5c4994
 ---> 882bcec61ccf
Step 3/6 : RUN apt-get update
 ---> Running in 324f28f384e0
/bin/sh: apt-get: not found
The command '/bin/sh -c apt-get update' returned a non-zero code: 127
3 Answers

Error:

/bin/sh: apt-get: not found
The command '/bin/sh -c apt-get update' returned a non-zero code: 127

Observation:

  1. This error comes when the container that you want to run is not Debian based, hence does not support 'apt'.
  2. To resolve this, we need to find out which package manager it utilizes.
  3. In my case it was: 'apk'.

Resolution:

  1. Replace 'apt-get' with 'apk' in your Dockerfile. (If this does not work you can try 'yum' package manager as well).
  2. Command in your Dockerfile should look like:

    RUN apk update

You can create a Dockerfile

FROM jenkins:latest
USER root
RUN apt-get update
RUN apt-get install -y python-pip
# Install app dependencies
RUN pip install --upgrade pip

You can build the custom image using

docker build -t jenkinspython .

Similar to what Hemant Sing's answer, but 2 slightly different things.

First, create a unique directory: mkdir foo "cd" to that directory and run:

docker build -f jenkinspython .

Where jenkinspython contains:

FROM jenkins:latest
USER root
RUN apt-get update
RUN apt-get install -y python-pip
# Install app dependencies
RUN pip install --upgrade pip

Notice that my change has -f, not -t. And notice that the build output does indeed contain:

Step 5/5 : RUN pip install --upgrade pip ---> Running in d460e0ebb11d Collecting pip Downloading https://files.pythonhosted.org/packages/5f/25/e52d3f31441505a5f3af41213346e5b6c221c9e086a166f3703d2ddaf940/pip-18.0-py2.py3-none-any.whl (1.3MB) Installing collected packages: pip Found existing installation: pip 9.0.1 Not uninstalling pip at /usr/lib/python2.7/dist-packages, outside environment /usr Successfully installed pip-18.0 Removing intermediate container d460e0ebb11d ---> b7d342751a79 Successfully built b7d342751a79

So now that the image has been built (in my case, b7d342751a79), fire it up and verify that pip has indeed been updated:

$ docker run -it b7d342751a79 bash
root@9f559d448be9:/# pip --version
pip 18.0 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)

So now your image has pip installed, so then you can feel free to pip install whatever crazy packages you need :)

Related