Setting up Jenkins with Dockerfile

Shivangi Sharma
4 min readJul 24, 2020

Jenkins is a free and open-source automation server. It helps automate the parts of software development related to building, testing, and deploying, facilitating continuous integration and continuous delivery.

Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

In this project-

  • Created a docker file to set up and run the Jenkins server in a container
  • Configured Jenkins to use SSH and SMTP servers
  • Created a job that pulls the Github repository containing the code file automatically when some developers push to Github.
  • It recognizes the type of code provided by the user and launches a particular container with a respective language interpreter.
  • Created another job to check if the build was successful or not, in case of failure send an email to the developer with error messages.

Dockerfile

Create the Dockerfile as follows-

FROM centos:7
RUN yum install java-11-openjdk.x86_64 -y
RUN yum install wget -y
RUN yum install httpd -y
RUN yum install git -y
RUN wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
RUN rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
RUN yum install jenkins -y
RUN yum install openssh-server -y
CMD java -jar /usr/lib/jenkins/jenkins.war

Build the Jenkins image as follows-

docker build -t myjenkins .

Now a new image has been formed with Jenkins installation

Create a new folder that will store all the files of Jenkins

mkdir /root/Desktop/myjenkins

Run the container with Jenkins image

docker run -dit --name sjenkins -p 8091:8080 -v /root/Desktop/myjenkins/:/root/.jenkins/ myjenkins:latest

Jenkins by default runs on port 8080 so we have used 8091 port to enable access to the container from outside world.

Configuring Jenkins

selecting plugins to install

Install the SSH plugins which would be used later-

  1. SSH Build Agents
  2. Publish over SSH
  3. SSH
SSH plugins installation

Install the Github plugins which would be used later-

  1. GitHub Branch Source
  2. GitHub
  3. Pipeline: GitHub Groovy Libraries
GitHub Plugins

Admin User can also be created

Create User

Jenkins has now been set up successfully!!

Jenkins ready to use

Setting up SSH host

To add SSH host in Jenkins, follow below-given steps:

  1. Go to Jenkins → configuration → SSH remote Hosts
  2. Give the host IP
  3. Set the port to 22
  4. Create and add credentials that would provide access to the remote host
  5. Check connection
SSH configuration

Setting up SMTP for Email notifications

To set the SMTP server for Gmail follow the below-given steps:

  1. Go to Jenkins → configuration → Extended Email Notification
  2. Mention smtp.gmail.com under SMTP server

3. Under Email Notification

4. Go to Advanced setting

5. Select SMTP authentication

6. Mention the username and password to access Gmail account

7. For Gmail, select SSL

8. Give the SMTP port 465

9. Test configuration by adding recipients ID

SMTP setup

Now we are all set to create our jobs!!

JOB1

The first job is the JOB1, it performs the following tasks -

  • Pulls the GitHub repository containing code file
  • Copies the data stored in the job1 workspace to another folder created called github_data
  • Changes the working directory to github_data folder
  • Identifies the type of code file uploaded by the user
  • Launches the respective container with the language interpreter
  • The job gets triggered whenever a new code is pushed to the repository by setting up a GitHub hook trigger
JOB1

Follow the below-given steps to create JOB1-

  1. Configure the SCM section and add the repository URL and credentials.
  2. Configure the GitHub webhook to use the GitHub hook trigger for GITScm polling.
  3. give the command under Execute shell script on remote host using SSH in the Build section.

JOB2

The second job tests the code, in case of any error the developer is informed by Email notification with the error message.

Give the command under Execute shell script on remote host using SSH in the Build section, add the E-mail Notification in the Post-build action and choose to send an email for every unstable build. Job2 is triggered by Job1.

JOB2

In case an error occurs in the code then an email is sent to the developer with the error code-

error email

--

--