Automatic code Deploy in docker with Jenkins

Mihir Patel
3 min readJun 17, 2021

--

Description:

In this blog you see When developer push code on Github then code automatic deployment in docker with help of jenkins . I use Shell script for docker and run and detect docker is install or not in machine .

Job Creation :

Step To create Job :

  1. Create Job
  2. Select freestyle
  3. Click ok

=> I created Job1 .

Now Configure Git Jenkins :

Job run periodically :

Execution of Shell :

Docker.sh (Shell Script) For docker detect and run and Jenkins job is use for run job periodically .

rpm -q docker-ce
x=$(echo $?)
if [[ $x == 0 ]]
then
sudo systemctl start docker
sudo docker run -dit — name os2 -p 8085:80 centos:7
sudo docker exec -i os2 yum install httpd -y
sudo docker exec -i os2 yum install net-tools -y
sudo docker cp /root/githubcode/ os2:/var/www/html
sudo docker exec -i os2 /usr/sbin/httpd
else
echo “[docker]
name=docker
baseurl=https://download.docker.com/linux/centos/7/x86_64/stable/
gpgcheck=0
“ > /etc/yum.repos.d/docker.repo
fi

Docker Script do the following:

This Above code will create a

  1. docker repo
  2. Install Dokcer
  3. Launch Container
  4. Copy html files in /var/www/html
  5. Restart httpd services

Now Click on Build Now : (So our Job Run )

Output :

Go to into Docker and See the Code which pull from the github by jenkins .

Now see Our Website is live :

-> Put HostIP:8085

Note : If you face any issue about denied then change /etc/sudoers file

and write Below Line

jenkins ALL=ALL NOPASSWD:ALL

Thank you ….

--

--