13

I have pulled jenkins container from docker hub like this:

docker pull jenkins

The container runs and I can access Jenkins UI in :

http://localhost:8080

My question is:

If I want to be able to create a jenkins job that pulls from a github repo and I want to run some python tests from one of the test files of that repo, how can I install extra packages such as virtualenvwrapper, pip, pytest, nose, selenium etc?

It appears that the docker container does not share any reference with local host file system.

How can I install such packages in this running container?

Thanks

0

1 Answer 1

20

You will need to install all your dependencies at docker container build time.

You can make your own Dockerfile off of the jenkins library, and then put custom stuff in there. Your Dockerfile can look like

FROM jenkins:latest
MAINTAINER Becks

RUN apt-get update && apt-get install -y {space delimited list of package}

Then, you can do something like...

docker build -t jenkins-docker --file Dockerfile .
docker run -it -d --name=jenkins-docker jenkins-docker

I might not have written all the syntax correctly, but this is basically what you need to do. If you want the run step to spin up jenkins, follow along with what they are doing in the existing Dockerfile here and add relevant sections to your dockerfile, to add some RUN steps to run jenkins.

Came across this page, which approaches a similar problem, although it also mounts the docker sock inside another container, to kind of connect one container to another. Given that its an external link, here's the relevant dockerfile from there,

FROM jenkins:1.596

USER root
RUN apt-get update \
      && apt-get install -y sudo \
      && rm -rf /var/lib/apt/lists/*
RUN echo "jenkins ALL=NOPASSWD: ALL" >> /etc/sudoers

USER jenkins
COPY plugins.txt /usr/share/jenkins/plugins.txt
RUN /usr/local/bin/plugins.sh /usr/share/jenkins/plugins.txt

And this is how you can spin it up.

docker build -t myjenk .
...
Successfully built 471fc0d22bff
$ docker run -d -v /var/run/docker.sock:/var/run/docker.sock \
                -v $(which docker):/usr/bin/docker -p 8080:8080 myjenk

I strongly suggest going through that post. Its pretty awesome.

3
  • 3
    While Debosmit'answer is the clean way to go, you can also do it in a hacky way. Launch jenkins, then connect inside docker exec -it container_name_or_id bash and do the same apt-get update && apt-get install -y {space delimited list of package} then disconnect from the container, then docker commit container_name_or_id myuser:myjenkins see the doc docs.docker.com/engine/reference/commandline/commit Now you have a shell inside, so you can Commented Jul 19, 2017 at 6:19
  • 4
    You should really not use docker commit as dockerfiles are better for maintenance and documentation. Especially for something as simple as installing packages in a container. Commented Jul 19, 2017 at 9:20
  • @WassimDhif, I fully agree with you but ... In the event you are working in an environment without internet at build and deploy time, aptitude will not work. Providing a solution to build a container with all requirements then commit to a private registry sounds like a viable option.
    – Nick.T
    Commented Jan 21, 2022 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.