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

Shirsha Datta
4 min readSep 1, 2020

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:

pip3 install ansible -y
ansible --version

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.

vim /etc/myhosts.txt
vim /etc/ansible/ansible.cfg

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.

ansible --version

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:

ansible all --list-hosts

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:

- hosts: dockerhost
gather_facts: false
tasks:
- name: conf yum for docker
yum_repository:
name: dockerrepo
baseurl: https://download.docker.com/linux/centos/7/x86_64/stable/
description: my docker repo desc
enabled: true
gpgcheck: 0
- name: install docker software
command: "yum install docker-ce --nobest -y"
- name: start docker services
service:
name: "docker"
state: started
enabled: yes
- name: install software python36
package:
name: python36
state: present
- name: install docker python library
pip:
name: docker-py
- name: pull image from hub
docker_image:
name: httpd:latest
source: pull
- name: run_container
docker_container:
name: myweb
image: httpd:latest
state: started
ports:
- "8082:80"
tty: true
detach: true

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

ansible-playbook docker.yml

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

docker ps
docker exec -it myweb /bin/bash

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:

curl 192.168.0.12:8082

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

curl 192.168.99.103:8082/myapp.html

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.

--

--

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.