Auto-deploy of Containers

Using Jenkins and Docker

VermaNikita
5 min readSep 14, 2020

❗️TASK-DESCRIPTION❗️

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

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

3. Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins

4. Job1 : Pull the Github repo automatically when some developers push 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 app is not working , then send email to developer with error messages.

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

STEP 1 : Creating a Dockerfile for Jenkins

Firstly create a folder or workspace in which we put all the codes(in my case it is /dtask2) where we want to keep it.

First we have to create a Dockerfile with following instructions in it .


$ vim Dockerfile
Note that:- Name of file should be "Dockerfile"

Dockerfile should be like this:-

FROM centos
FROM yum install wget -y
FROM yum install sudo -y
FROM yum install net-tools-y
FROM sudo wget -o /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
RUN sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
RUN yum install java-11-openjdk.x86_64 -y
RUN yum install jenkins -y
RUN install httpd -y
RUN yum install git -y
CMD systemctl start jenkins
EXPOSE 8080
CMD /usr/sbin/httpd -DEFOREGROUND
CMD sudo /etc/init.d/jenkins start
CMD sudo /etc/init.d/jenkins status
RUN echo 'jenkins ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
CMD java -jar /usr/lib/jenkins/jenkins.war

Build an image by following command →

$ docker build -t myjenkins:n2 .NOte that:- "." gives the current path of Dockerfile .So don't forget to put it there or write an absolute path there.$ docker images 

Now launch a container for getting the dashboard of Jenkins by using following command →

$ docker build -t -p 8095:8080 -v /dtask2:/cdtask2 --name nikjenkins myjenkins:n1Note that:- "-p" option is used for NATING purpose , "-v" is used for using Persistent Volume concept ,"--name" is used for giving the name for container and lastly "myjenkins:n1" is the source image in my case.

Check the container list by using command →

$ docker ps

Go to the link of your system by “ifconfig” and port number that you assigned at the time of launching conatiner.

Now Start using Jenkins and create another user by follow the step :- Manage Jenkins →Manage Users →Create Users then write your credentials and then login.

Now go to the plugins and download the necessary plugins for this task:-

(i)github

(ii)build pipeline

(iii)ssh

At the time of downloading plugins , you can push your code on Github using Git Bash as:-

Github looks like:-

Now come to the jenkins and create the jobs:-

JOB1:-Pull the Github repo automatically when some developers push repo to Github.

Remember in above BUILD part that you should give the path (inside the docker container_in my case i created at the time of launching container through docker) to copy.

Output of above task is as →

We can check it manually that whether our target file is copied at the particular folder by going inside the container in which jenkins run by the following commands→

$ docker container exec -it nikjenkins /bin/bash$ ls$ cd cdtask2$ ls

Firstly,we have to install docker for jenkins container and attach it with socket by using following command:-

$ docker container exec -it nikjenkins yum install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-17.12.0.ce-1.el7.centos.x86_64.rpm
[Note:- Take your version of centos and docker same,I used centos latest]

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

JOB3:-In this job, we would be checking whether the code is working or not. It’s a testing environment.

JOB4:-Now this is job where , if the could would not be working then we will be sending an Email to the admin of the Jenkins or the email address provided in the Jenkins .

So through this we would be able to know whether the code is working or not.

JOB5:-The last job will be monitoring our containers. A need container will be launched if our container terminates or stops.

Thank you !!!

--

--

VermaNikita

Mistakes increase your experience and experience decrease your mistakes … If you learn from your mistakes then other will learn from your success…