DevOps Project — CI/CD -2

In this blog, we are going to build and deploy our application on Docker Container.

jabir ahammed
4 min readAug 9, 2023

In the last blog, we have an environment with Git, GitHub, Jenkins, Maven, Tomcat, we can able to commit our code on to GitHub, and Jenkins is going to pull the code from GitHub. Build with the help of Maven and deploy it on a Tomcat server.

Next thing, we are going to see how we can deploy our artifacts on a Docker container.

For this, we are going to use GitHub Jenkins Maven and Docker. In the section. We are going to see how to set up Docker environment why because GitHub, Jenkins Maven is already exist, then we are going to see how to write a Dockerfile next, creating an image and a container on Docker host.

Link to last blog : https://medium.com/@ahammed.jabirp/devops-project-ci-cd-1-35db2050cdf3

Github : https://github.com/jabir000/hello-world.git

We already have this environment :-

So now we are going to setup a docker environment. for that we need to setup a dockerhost and on top of that we can install docker so that run Docker containers.

Setup CI/CD with Github, Jenkins, Maven and Docker

1- Setting up Docker environment

2- Write Dockerfile

3- Create an image and container on docker host

4- Integrate docker host with Jenkins

5- Create CI/CD job to build and deploy on a container.

To initiate , the first step involves setting up a Docker server.

Connect to terminal and install docker.

sudo su - 
yum install docker -y
systemctl start docker
systemctl enable docker
systemctl status docker
useradd dockeradmin
passwd dockeradmin
usermod -aG docker dockeradmin
vim /etc/ssh/sshd_config
( PasswordAuthentication yes )
systemctl restart sshd

Lets Integrate Docker with Jenkins

Jenkins -> Manage Jenkins -> Plugins -> Available -> Search for Publish Over SSH -> Install

Lets configure Docker in Jenkins

Jenkins -> Manage Jenkins -> System -> Publish Over SSH -> SSH Servers -> Add -> Give Name -> Hostname : <Docker host private ip> -> Username : dockeradmin -> advanced -> enable password based authentication -> password : <password of dockeradmin -> test configuration -> Apply -> Save

Successfully instegrated docker with jenkins.

Now goto terminal

cd /opt
mkdir docker
cd docker
chown -R dockeradmin:dockeradmin /opt/docker
vim Dockerfile
FROM tomcat:latest
RUN cp -R /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps
COPY ./*.war /usr/local/tomcat/webapps

chmod 777 /var/run/docker.sock

Now Create a Jenkins job to pull the code from Github , build with help of Maven and copy the Artifacts to Dockerhost.

Instead of creating a new job we can copy the configuration from buildanddeployjob that we have created for Tomcat Server.

Jenkins -> New Item -> Name: buildanddeployjoboncontainer -> Copy from: buildanddeployjob -> OK

Jenkins -> Dashboard -> buildanddeployjoboncontainer -> General -> give a description -> Remove Deploy war/ear to a container from Post-build Actions -> Post-build Actions -> select Send build artifacts over SSH -> SSH Server -> Name : give name of Added SSH Server -> Source file: webapp/target/*.war -> Remove prefix : webapp/target -> Remote directory : //opt//docker -> Exec command : enter the below commands

cd /opt/docker;
docker build -t regapp:v1 .;
docker stop registerapp;
docker rm registerapp;
docker run -d --name registerapp -p 8081:8080 regapp:v1

Apply -> Save

Now lets modify the Source Code

Open GitBash

cd hello-world/webapp/src/main/webapp/
vim index.jsp

Lets make some changes.

Changed this

Into this

Save.

git status
git add .
git status
git commit -m "change background colour to blue"
git push origin master

Now the build is automatically triggered.

In this section, we have successfully deployed the application to the Docker server and enabled CI/CD. If any changes occur in the source code, they will be automatically built and deployed to the server.

In the next session, We are going to use Ansible as a Deployment tool and will be segregating the work, which has to be done by Jenkins, which can be done by Ansible.

That is how we can take leverage off configuration management tool as a deployment tool.

Linkedin : www.linkedin.com/in/jabir-ahammed

THANK YOU !

--

--