Deploying a Web Application with Ansible and Docker Compose
Introduction
In today's world, automating the deployment process is crucial for ensuring consistency, reliability, and speed. In this article we will walk through steps to automate the deployment of web application using Ansible and Docker Compose.
Prerequisites
Ansible installed on control node.
Docker and Docker Compose installed on your Target Servers.
Steps:
Step-1:
First we need to create an inventory.ini file in which we will define the hosts details where Docker and Docker Compose should be pre-installed.
[web_servers]
IP_address_of_target_servers1 ansible_user=your_username ansible_ssh_private_key_file=path/to/your/private/key
IP_address_of_target_servers2 ansible_user=your_username ansible_ssh_private_key_file=path/to/your/private/key
Step-2:
Here we need to write playbook.yml file which will include below steps.
It will pull latest code from git repository.
It will build and deploy Docker Compose services.
It will check status of deployed application, whether its running or not.
---
- name: Deploy the application
hosts: web_servers
become: yes
tasks:
- name: Pull latest code from repository
git:
repo: 'your-git-url'
dest: <path-of-webapp-code>
- name: Build and deploy Docker Compose services
command: docker-compose up -d
args:
chdir: <path-of-webapp-code>
- name: Check application status
uri:
url: http://localhost:8000
status_code: 200
Note:
Your Git repository should have docker-compose.yml file and webapp code so that it can install and check the status of services.
Step-3:
Now we need to give below command on control node where ansible is installed to deploy web application on target servers.
ansible-playbook -i inventory.ini playbook.yml
Subscribe to my newsletter
Read articles from Gaurav Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Gaurav Kumar
Gaurav Kumar
I am working as a full time DevOps Engineer at Tata Consultancy Services from past 2.7 yrs, I have very good experience of containerization tools Docker, Kubernetes, OpenShift. I have good experience of using Ansible, Terraform and others.