Developing web-apps and deploying them on top of docker containers using Ansible Playbooks

One of the most needed requirement nowadays is to deploy your application in docker containers. But for that one needs to know docker and launch their application every time. So this becomes cumbersome. So in order to automate the process, we use Ansible as our automation tool.

So let us start with what is ansible:

Ansible: Ansible is the simplest solution for configuration management available. It’s designed to be minimal in nature, consistent, secure and highly reliable. Ansible relies on the most secure remote configuration management system available as its default transport layer: OpenSSH.

Tools and software required:

  1. 2 RHEL8 as our OS
  2. RedHat Ansible 2.9.11
  3. Python 3.6.8

So let us break our requirement into simple steps:

We need one controller node which will be our RedHat VM. There we need to have Ansible(pre-requisite is Python 3.6.8 should be installed). To install follow the below-given steps:

We see that there is no inventory linked to it, therefore we need to create one config file and then add it to the inventory. In the config file, we need to specify the IP of the managed node i.e the node where we want our docker services to run.

Now if we run the command the below command we find that all the nodes the configuration file shows up. It also shows the version of python used by Ansible.

We are done with the one-time setup of ansible in the controller node. Now we can deploy our application in docker containers using ansible. For this, I have used 2 different VM’s but you can also do in one VM.

To check all the hosts available:

Now, let us start by writing the playbook. It is a file written in YAMLlanguage.

Step1: configure docker repo for yum

Step2: install docker software

Step3: start docker services

Step4: install python3 to get pip command

Step5: install docker python library

Step6: pull image from docker hub

Step7: run a container

You can access the full code from here:

Now in one click, your setup will be configured, by running this command:

To check if everything is done correctly in the managed node, go to that VM and run the below-given commands:

Then you execute the docker container to check all the files and create your own file:

You can check from the controller node if the container is launched using the curl command:

You can also access the page with other network cards attached to the managed node:

The GitHub repo for the same:

We are finally done with the implementation. If you have any doubt feel free to connect, I will be glad to help you out.

Thank you.

--

--

I am a DevOps Enthusiast and recently taken to Cloud Computing. Learning Flutter App Development currently. In my free time I engage in competitive coding.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shirsha Datta

I am a DevOps Enthusiast and recently taken to Cloud Computing. Learning Flutter App Development currently. In my free time I engage in competitive coding.