Pages

Wednesday, March 13, 2019

Jenkins - Running Inside a Container

Every now and then i come across a requirement to run a continuous integration tool inside a docker container. Most of the times , the need for running jenkins inside a container is not required. Running Jenkins inside a docker container does not provide any special benefits other than what container provide. In this article we will see how we can run a Jenkins inside a docker container and how we can automate certain things for Jenkins.

Process 1 - Running a jenkins container is quite easy. All we have to do is to download the Jenkins image and run it exposing a specific port as below,
docker run -d -p 18080:8080 -p  50000:50000 jenkins

We can then access the jenkins server on the local machine port 18080 on the Url “http://localhost:18080” and then we will be redirected to the jenkins initial page. The first page it self asks for the key where we
Now we have to identify the Jenkins container running using the “docker ps” and then run the exec command on the running container to get the above Admin password as below,
jagadishHome$Tue Mar 12@ docker exec 00daf58feeec cat /var/jenkins_home/secrets/initialAdminPassword
4af47a8c92854c48912c8dda6265dcfc

00daf58feeec is the running container. From this the installation is quite easy. If asks for the plugin that we need to install, then it asks to create a user etc much like the jenkins installation.

Process 2 - In the second process we will use a docker build to build the jenkins image by automating the plugin installation , user creation and some other steps.

1. Create the Docker file with the below content
# Starting off with the Jenkins base Image
FROM jenkins/jenkins:latest
# Installing the plugins we need using the in-built install-plugins.sh script
RUN install-plugins.sh git matrix-auth workflow-aggregator docker-workflow blueocean credentials-binding
# Setting up environment variables for Jenkins admin user
ENV JENKINS_USER admin
ENV JENKINS_PASS admin
# Skip the initial setup wizard
ENV JAVA_OPTS -Djenkins.install.runSetupWizard=false
# Start-up scripts to set number of executors and creating the admin user
COPY executors.groovy /usr/share/jenkins/ref/init.groovy.d/
COPY default-user.groovy /usr/share/jenkins/ref/init.groovy.d/
VOLUME /var/jenkins_home

If you are familiar with the Dockerfile, the instructions are quite easy to understand. All we do is to make Jenkins as a base image, Run the script “install-plugins.sh” script by passing the plugins that i need to install.

The Third step is the user and password setting for the jenkins. In this case i am passing them as arguments and the username and password in this case is “admin”. In the last steps we will copy 2 scripts one is the “executores.groovy” script for setting the executors and other one is the “default-user.groovy” script which will create the users Jenkins.

We also added a Volume and also set a Env JAVA_OPTS. All 3 scripts executores.groovy, default-user.groovy and install-plugins.sh should be in the same location where the Dockerfile exists. Once you built the image, we can then run the container as same above 
docker run -d -p 18080:8080 -p  50000:50000

Once the container is ran and we access the “localhost:18080”, we will be taken directly to the login page. We can then use the “admin” and “admin” for accessing the jenkins server.

We can automate as much as we can in this second process. This is by far the best method.

More to come , Happy learning :-)

No comments :

Post a Comment