Docker-in-Docker jenkins Integration

Amangupta
4 min readJul 17, 2020

--

Here we are doing the integration of docker in docker with jenkins and github for push the code. I am performing this task given below:-

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

firstly i build a Docker Image by using a Dockerfile in which the Docker and jenkins are running. The Code of Docker file is Given below:-

FROM centos
RUN yum install wget -y
RUN yum install sudo -y

#jenkins installation
RUN wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
RUN rpm — import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
RUN yum upgrade -y

RUN yum install java-1.8.0-openjdk-devel -y
RUN yum install jenkins -y
RUN echo “jenkins ALL=(ALL) NOPASSWD:ALL” >> /etc/sudoers

# some softwares to run docker from inside
RUN yum install ca-certificates -y
RUN yum install curl -y
RUN yum install gnupg2 -y
RUN yum install dnf -y

# setup repository for docker
RUN dnf install ‘dnf-command(config-manager)’ -y
RUN dnf config-manager — add-repo=https://download.docker.com/linux/centos/docker-ce.repo -y

#some basic software
RUN yum install net-tools -y

# install docker
RUN yum install docker-ce — nobest -y
RUN sudo usermod -a -G docker jenkins

#install git

RUN yum install git -y

# this is the most important command
CMD chmod 777 /var/run/docker.sock && java -jar /usr/lib/jenkins/jenkins.war

EXPOSE 8080

dockerfile for building image

after building a image in which jenkins and docker is running. then we launch the container by using this image:-

docker run -it -p 8080:8080 -p 50000:50000 -v /var/run/docker.sock:/var/run/docker.sock -v /jenkins_home:/var/jenkins_home — name jenkins_docker amangupta2562/docker-jenkins:1

then the password appear for login in jenkins. then in jenkins for buiding the we need some plugin like github, delivery pipeline, build pipeline ,ssh agent plugins,etc,..

use the default password
plugins installation

then I start build jobs as follows:-

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

github repository used as SCM
use github trigger to build this job
use tunneling(use ngrok) for github webhok

job1 complete!

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 should start the container that has PHP already installed)

this job is triggered when job1(github_pull) build stable
container launched for hosting website

job2 complete!

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

ssh agent setup
use ssh agent for teting the website is running or not.

job3 build after job2 build stable.

job3 complete!

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

cofigure the email-notification setup
google account setup of security for sending mail…

job4 complete!

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.

this job triggers periodically to check the website hosting container

job5 complete!

build pipeline of job chain:-

task complete!!!!

--

--

No responses yet