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:
- 2 RHEL8 as our OS
- RedHat Ansible 2.9.11
- 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
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:
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
- name: conf yum for docker
description: my docker repo desc
- name: install docker software
command: "yum install docker-ce --nobest -y"
- name: start docker services
- name: install software python36
- name: install docker python library
- name: pull image from hub
- name: run_container
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:
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:
You can also access the page with other network cards attached to the managed node:
The GitHub repo for the same:
Developing web-apps and deploying them on top of docker containers using Ansible Playbooks. GitHub is home to over 50…
We are finally done with the implementation. If you have any doubt feel free to connect, I will be glad to help you out.