Create Jenkins Docker image with pre-configured data


Background: Docker is an open-source project for automating the deployment of applications as portable, self-sufficient containers that can run on the cloud or on-premise. So, now we are going to setup Jenkins with pre-configured data.

We are going to pack the pre- configurations as a separate volume and the let Jenkins container use that volume. The advantage of using a volume is that the volume can exist independently of the Jenkins container, and also this volume can be used with other container without any additional overtime. And we can copy the contents from the volume to the HOST OS.

So first let’s begin with creating the volume.
We will create a Dockerfile for the same.
First, we will create a base image from ubuntu.
Then, we change the user to root.
We then create directories – “/var/Jenkins_home”, “/var/Jenkins_home/jobs”, “/var/Jenkins_home/plugins”
Note: We are running Jenkins from /var/Jenkins_home
We then create Jenkins user to match with Jenkins user that is created while running Jenkins.
We then copy the data from our existing Jenkins installation. We copy the contents from the jobs folder and plugins folder. (We are just pre-configuring Jenkins jobs information and plugins information. We can follow the same procedure for other configurations.)
We then change the ownerships of the folder to Jenkins user
We then mount the volumes that we have created.


The below is the complete Dockerfile

FROM ubuntu

USER root

# Create the jenkins user
RUN useradd -d "/var/jenkins_home" -u 1000 -m -s /bin/bash jenkins

# Create the folders and volume mount points
RUN mkdir -p /var/jenkins_home/jobs
RUN mkdir -p /var/jenkins_home/plugins

ADD data/jobs/ /var/jenkins_home/jobs
ADD plugins/ /var/jenkins_home/plugins

RUN chown -R jenkins:jenkins /var/jenkins_home/jobs
RUN chown -R jenkins:jenkins /var/jenkins_home/plugins

VOLUME ["/var/log/jenkins", "/var/cache/jenkins", "/var/jenkins_home", "/opt/maven"]

CMD ["echo", "Data container for Jenkins - New version"]
We then run Jenkins image provided by Docker attaching the Volume that we have created.
CMD docker run -p 8080:8080 -p 50000:50000 --name=jenkins-master --volumes-from=jenkins-data -d jenkins
For copying the contents from Docker volume into the host OS, use the below command.
CMD docker run -p 8080:8080 -p 50000:50000 --name=jenkins-master --volumes-from=jenkins-data -d jenkins

Comments

Popular Posts