We have deployed insurance application in capstone project2 using jenkins, github, dockerhub,docker in AWS EC2 Machine, we will take the same application and deploy through Azure Devops, Azure Repos, Azure container registry, docker in VM machine. I have done the below steps to complete this project:
We will create a new project in Azure DevOps and then go to Repo and click on Import a Repository:
We have already created gihub repo for the capstone2 project:
https://github.com/kotianrakshith/CapstoneProject2
We will import this:
We can see after the import all the files are imported:
(Later we will delete all the files not needed ex. Jenkinsfile,ansible playbook)
We wiill go to pipeline and create a new pipeline:
Then select your repo and the code:
Then we will chose maven and only chose package:
So our code till the build stage will be as below: (I have used self hosted pool instead of microsoft as Im not yet approved for it)
Now lets save and run:
As I'm using self hosted agent it will ask for permision:
Once you permit, it will run and we can see that it has sucessfully run and maven has build package to be deployed.
First let us create a container registry:
Go to container registries in azure portal and click create container registry:
Give all the details and click review+create
Now we will go the pipeline and add the build and push to container regstry steps:
Edit the pipeline and add the task buildandpush docker:
The code will be as below:
We have made sure docker is running locally and we will save the pipeline and it should run successfully:
Now let us check the Azure Container Registry if the image has been stored or not.
Go to the registry and navigate to Repositories, you should be able to see newly create ‘insuranceapp’ image
Now since we have to deploy the docker container in a virtual machine, we will create a virtual machine. Go to virtual machine in azure and click create new:
We will give sufficient details:
Then we will review and create.
Once created, we will go to the networking and all inbound rule:
Then we will add inbound 8081 as this is the port we will be exposing our docker container application.
Now there should be SSH port and 8081 port :
Now lets connect to the VM and install docker.
In this case we are connecting through bastion as its safer. In the bastion, it may ask you to setup bastion, you can click on deploy bastion which automatically will set it up and add new subnet:
Once deployed you can connect using your ssh key
Your vm will open in the new browser tab:
Now let us install docker.
First we upgrade the system
sudo apt update && sudo apt upgrade -y
Now we install the packages, keys, required repositories:
sudo apt install ca-certificates curl gnupg lsb-release
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb\_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Now run the update command again('sudo apt update')
Now install the docker ce:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Now check the docker version:
docker -v
If you check the docker you see it is running:
Then you can add your user for docker group(This may not be needed, but if you use different user it is helpful)
sudo usermod -aG docker $USER
Then reload the session using command: newgrp docker
Now we will also do docker login to login to azure container registry:
First we will go to access keys and enable admin user which will give us username and password
We wll use this to login to login server.
docker login kotianrakshith.azurecr.io
Go to the Project Settings-> Service connections-> click New service connection:
Search for SSH:
Give all the required details:
Then save and it should be added:
We will deploy docker container through docker commands given SSH connections to the VM.
Go to the pipline and edit the pipeline. Search for SSH:
We will select the service connection we added and add following command as a scirpt:
docker stop insuranceapp 2>/dev/null
docker rm insuranceapp 2>/dev/null
docker run -itd -p 8081:8081 --name insuranceapp kotianrakshith.azurecr.io/insuranceapp
(We need stop and rm if we want to run this pipeline repeatedly as once run there will be container present and we are using 2>/dev/null so it can ignore the errors)
Now lets save and it will run:
We see that it has run sucessfully.
First lets go and check the VM if the app is deployed in docker:
We can see from docker ps
command that it has been created and running correctly.
Now let us access the app and check. To access it we need to use ip:port so here it is
<http://20.115.123.210:8081/>
We can see that we are able to access it correctly:
That completes this project. We have used the same code from capstone2 but we have used azure tools like Azure Devops, Azure Repos, Azure container registry, docker in VM machine to achive the same results much easily.