DevOps Project — CI/CD -3

In this blog, we are going to build and deploy our application on Docker Container with the help of Ansible.

jabir ahammed
6 min readAug 10, 2023

In the previous blog, we discussed the process of making changes on our workstation, followed by committing the changes using Git onto GitHub.

Once the latest code is available on the Git repository, Jenkins is capable of fetching the updated code, initiating a build using Maven, creating Docker images, and subsequently deploying these Docker containers onto the Docker host.

Link to last blog : https://medium.com/@ahammed.jabirp/devops-project-ci-cd-2-5726966c20d2

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

In that case, we used Jenkins as a tool for both building and deploying applications.

However, are there any more efficient methods to manage this process?

Yes, that is where we can introduce deployment tool.

We are going to use Ansible as a deployment tool , so that Jenkins need not to do the administrative kind of activities.

Because Jenkins is more efficiently work as a build tool along with Ansible.

So in this case Jenkins is going to take the code from GitHub and build artifacts and copy those artifacts onto Ansible server.

Now it is Ansible’s task to create images and deploy the containers.

Ansible is going to take the artifact and with the help of Docker file it creates a Docker image.

This Docker image, we can commit it into the Docker hub because Docker Hub is a repository to store Docker images.

Now whenever we execute any Ansible playbook to deploy a container, this Docker host communicates with the Docker hub and pull the image whatever we mentioned in our playbook and create a container out of it.

We already have this environment :-

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

1- Setup Ansible server

2- Integrate Docker host with Ansible

3- Create Ansible playbook to create image

4- Create Ansible playbook to create container

5- Integrate Ansible with Jenkins

6- CI/CD job to build code on Ansible and deploy it on Docker container

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

For that we need to create an ec2 instance

connect to the Ansible terminal

sudo su -
useradd ansadmin
passwd ansadmin
vim /etc/ssh/sshd_config
( PasswordAuthentication yes )
visudo
( ansadmin ALL=(ALL) NOPASSWD: ALL )
systemctl restart sshd
sudo su - ansadmin
ssh-keygen
sudo su -
amazon-linux-extras install ansible2 -y
python --version
ansible --version

Lets Integrate Docker with Ansible

Connect to Docker host terminal

sudo su -
useradd ansadmin
passwd ansadmin
visudo
( ansadmin ALL=(ALL) NOPASSWD: ALL )

Go to the Ansible terminal

sudo su -
vim /etc/ansible/hosts

Clear everything in this file and add this :-

[docker]
< docker private ip >

save it.

sudo su - ansadmin
ssh-copy-id < docker private ip >
ansible all -m ping

Lets Integrate Ansible with Jenkins

Jenkins -> Manage Jenkins -> System -> Pubish Over SSH -> Add -> SSH Server -> Give Name (ansible-server)-> Hostname : < ansible private ip address > -> Username : ansadmin -> Advanced -> enable password authentication -> Password : ( give the password of ansadmin ) -> Test Configuration.

Integration of jenkins with Ansible has been successfull.

Now lets create the jenkins job

Jenkins -> New item -> Name : copyartifactstoansible -> Copy from :

buildanddeployjoboncontainer -> OK

Goto Ansible server terminal

sudo su - ansadmin
cd /opt
sudo mkdir docker
sudo chown ansadmin:ansadmin docker

Jenkins -> Dashboard -> buildanddeployjoboncontainer -> General -> give a description -> Post-build Action -> SSH Server -> Name : select the name that given for ansible (ansible-server) -> Clear the coloumn of Exec Command -> Apply -> Save

Goto Ansible server terminal

sudo yum install docker -y
sudo usermod -aG docker ansadmin
sudo systemctl enable --now docker
cd /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
sudo chmod 777 /var/run/docker.sock
sudo vim /etc/ansible/hosts
[docker]
< docker private ip >

[ansible]
< ansible private ip >

save it

ssh-copy-ip < ansible private ip >
sudos u - ansadmin
docker login

Enter Username and Password of Dockerhub account to login

cd /opt/docker
vim regapp.yml
---
- hosts: ansible
tasks:
- name: create docker image
command: docker build -t regapp:latest .
args:
chdir: /opt/docker
- name: create tag to push image onto dockerhub
command: docker tag regapp:latest jabirdocker/regapp:latest
- name: push docker image
command: docker push jabirdocker/regapp:latest

save it

ansible-playbook regapp.yml --check
vim deploy_regapp.yml
---
- hosts: docker
tasks:
- name: stop existing container
command: docker stop regapp-server
ignore_errors: yes
- name: remove the container
command: docker rm regapp-server
ignore_errors: yes
- name: remove image
command: docker rmi jabirdocker/regapp:latest
ignore_errors: yes
- name: create container
command: docker run -d --name regapp-server -p 8082:8080 jabirdocker/regapp:latest

save it

ansible-playbook deploy_regapp.yml --check

Goto Docker host terminal

sudo su -
chmod 777 /var/run/docker.sock

Jenkins -> Dashboard -> buildanddeployjoboncontainer -> Configuration -> Post-build Action -> SSH Server -> Exec commad : Enter the following command in the Exec command

ansible-playbook /opt/docker/regapp.yml;
sleep;
ansible-playbook /opt/docker/deploy_regapp.yml

Apply -> Save

Let’s make some changes in the source code.

Goto Gitbash terminal

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

Changed this

into this

save it

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

Goto Jenkins

we can see the build have triggered automatically.

Lets check Dockerhub account.

Lets check Docker host.

we can see the new image and container is created.

In this blog , We have configured our Jenkins job in such a way that if somebody modified code, it should automatically build the code, create the image, create the container and we could able to access those changes from the browser.

But the problem is whenever there are some changes, we are terminating the existing container and creating a new container. During this time, end user cannot able to access the application.

Another thing is if our container is terminated,

How we can come to know that it is not working out?

How we can create new container automatically?

We don’t have such kind of mechanisms over here.

That is where container management system comes into the picture.

In next section, we are going to use leverage of the container management system (Kubernetes) to run our containerised applications with higher availability and fault tolerance.

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

THANK YOU !

--

--