Automation with Custom Jenkins Image by using Dockerfile.

Tasks to Perform :

1. Create a container image that’s has Jenkins installed using Dockerfile.

2. When we launch this image, it should automatically start the Jenkins service in the container.

3. Create a job chain of Job1, Job2, Job3, and Job4 using the build pipeline plugin in Jenkins.

4. Job1: Pull the GitHub repo automatically when some developers push the repo to GitHub.

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

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

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

8. Create One extra job Job5 for monitor: If the container where the app is running. fails due to any reason then this job should automatically start the container again.

Lets start with creating the docker image using dockerfile.

# mkdir /ws3

FROM: the image to be used for container

RUN: commands to be executed while building the modified container

COPY: command will be used to copy files from host to image while building it.

CMD: the cmd used here will keep the Jenkins live till the container is on and will start on container boot.

now the Mail.py file :

Note : it should be made in the same directory.

Now, our Dockerfile is ready. Now we will build our own custom image. Its called build. We use the command: docker build -t <name of image>:<version> <name of the folder in which Dockerfile is located>

After this successful build, using cmd docker images we can see that a new image is created.

And after creating the image, create a container using that image using cmd.

# docker run -it — privileged -p 8085:8080 -v /:/host — name jenkinos1 myjenkinsos1:v1

— privileged has been used for special access to be used later while playing with dockers.

and when you create an image you will get the password required for Jenkins when you log in for the first time.

we need to put the obtained one time password in the administrator password field.

We will see the following Jenkins page. We can go in the manage user settings and change the default password.

Now, heading towards the jobs: We’ve to now build the 5 jobs, and then we will create a job chain of job1, job2, job3, and job4 using build pipeline plugin.

Before creating the jobs we first need to install Git, GitHub, and Build Pipeline plugins in jenkins.

Now, let’s get started with building jobs.

Job 1 :- Pull the GitHub repo automatically when some developers push the repo to GitHub.

Here, to mount another point for sharing files for deployment, I am copying files to host os.

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

here i have give the condition for the web pages with html extension, you can similarly provide mode conditions for pages like php, etc.

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

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

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

Here i have kept “ * * * * * “ because it will check the status of the container every second.

Finally, Build Pipeline View

GitHub Repo :- https://github.com/Tanmay4443/Automation-jenkins-docker.git

Thank you for Reading ~