Deploying and Testing Code inside Docker container from GitHub….

Parinati Rajput
5 min readSep 5, 2021

Task Description :

1. Create container image that has Jenkins installed using Dockerfile.

2. When we launch this image, Jenkins service automatically starts in the container.

3. Create a job chain of Job 1, Job 2, Job 3 and Job 4 using build pipeline plugin in Jenkins.

4. Job1 : Pull the Github repository automatically when developers push the Code to Github.

5. Job2 : By looking at the code or program file, Jenkins should automatically start the respective language interpreter installed image container to deploy code ( eg. If code is of PHP, then Jenkins start the container that has PHP already installed ).

6. Job3 : Test your app if it is working or not.

7. Job4 : if app is not working , then send email to developer with error messages.

8. Create an extra Job (Job 5) for monitoring basis : If container where app is running fails due to any reason, then this job should automatically start the container again.

Pipeline Structure

Firstly you need to docker installed on your computer, you can refer the article below :

After docker installation lets get started..

Let me describe a little bit what we are going to do in this task. We are going to run different jobs in pipeline inside jenkins automation server.

We will fetch the code pushed by developer from github,here we are using hooks inside git, which will automatically trigger jenkins about changes happend in github. Then we will launch a conatiner on which our code will run. We will test if our website is running or not if not then developer will be notified on email address.

Step 1: Create container image that has Jenkins installed using Dockerfile.

Save the file with the name “dockerfile”

Use the below command to build dockerfile…
[root@localhost parinati]# docker build -t myjenkins:v1 /home/parinati

check the installed image using “docker images”

after creating image run the image to create container..

[root@localhost ~]# docker run -it — privileged -p 8081:8080 -v/:/host — name je nkinsOS jenkinos:v1

You will get the secret key after creating the image,copy it and use for login…

First create a tunnel to the private world, we are using ngrok software to do so.

[root@localhost ~]# ./ngrok http 8081

Job1: Pull the Github repository automatically when developers push the Code to Github.

Open your github account and create a web hook there, using the below steps..

you can pull the “index.html” from my repo..

https://github.com/parinati789/jenkins-task1.git

Step 1:Go to repo >>Settings>> Webhooks

and insert the payload url as follows: [http:/ip:port/github-webhook]

Now go to jenkins Dashboard>>New item >>enter job1 and select “free style project”>>ok

Configure job 1 using the pictures below:

Now save the job and build it…

Job 2: By looking at the code or program file, Jenkins should automatically start the respective language interpreter installed image container to deploy code.

Job 3: Test your app if it is working or not.

Job 4: If app is not working , then send email to developer with error messages.

job 4 completed…

Job 5 : If container where app is running fails due to any reason, then this job should automatically start the container again.

When i tried with error code.. i recieved mail

Task Completed successfully….

Thanks for Reading !!

Follow me on linkedin : linkedin.com/in/parinati-rajput-646804171

Github : https://github.com/parinati789

--

--