Wednesday, November 6, 2024
HomeDevOpsHow to run a jenkins on Docker Container

How to run a jenkins on Docker Container

We usually spend more time on installing Jenkins on linux, which may cause you to waste your time. Here our handy tool docker has feature to install and configure Jenkins on docker. Now you can get the official Jenkins image from docker hub. Here you got pull the latest version of Jenkins;

# docker pull Jenkins

Sometimes running official Jenkins image not simple

# docker run -p 8080:8080 jenkins

This is very easy way to run the image, but in real time this is not sufficient. We may require more features like install some pre required plugins and data backup etc. here will see how we can add some features with this.

Building a Image with some Plugins

I found some quite useful formatted dockerfile and modified as per my requirement, in this I have mention the image name with some pre-required plugins required at starting. And added some permissions to Jenkins user, which is the default user handles all Jenkins job. I have added the required plugin in a text file and called the shell to install the plugins.

FROM jenkins

MAINTAINER foxutech

 

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

The ‘plugins.txt’ file that in my case looked like this:

# cat plugins.txt

dockerhub:1.0

scm-api:latest

git-client:latest

git:latest

greenballs:latest

now we have required details to run our dockerfile, here will start build;

# docker build -t myjenkins jenkins .

Now my image is ready, now I can start my container using below commend.

# docker run -d -p 8080:8080 –name custom-jenkins reponame/jenkins

Adding a Data Volume Container

Creating a named data volume container was pretty simple:

# docker create -v /var/jenkins_home –name jenkins-drive jenkins

This command uses the ‘/var/jenkins_home’ directory volume as per the official image and provides a name ‘jenkins-drive’ to identify the data volume container.

To use the data volume container with an image you use the ‘–volumes-from’ flag to mount the ‘/var/jenkins_home’ volume in another container:

# docker run -d -p 8080:8080 –volumes-from jenkins-dv –name myjenkins jenkins

Once you have the docker container running you can go to http://localhost:8080 to see the Jenkins instance running. This instance is storing data in the volume container you set up previously, so if you set up a job and stop the container the data is persisted. To prove this, you can stop and remove the ‘myjenkins’ container and start a second container:

# docker run -d -p 8080:8080 –volumes-from jenkins-dv –name myjenkins2 jenkins

Once more hitting http://localhost:8080 will show your Jenkins instance but this time any jobs you set up and that were stored to the data volume container should be present.

Backing Up

To backup the data from the volume container is simple to. Simply run:

# docker cp jenkins-dv:/var/jenkins_home /tmp/jenkins-backup

Once this operation is complete on your local machine in ‘/tmp/jenkins-backup’ you will find a ‘jenkins_home’ directory backup. You could now use this to populate a new data volume container.

RELATED ARTICLES
- Advertisment -

Most Popular

Recent Comments