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
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
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
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
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
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