Using Ansible to launch Webserver inside a Docker Container!

This blog aims to explain the process of launching a webserver using containerization technology(Docker) and the DevOps tool(Ansible) for automation!

Automation is been widely adopted & in huge demand these days due to multiple reasons, & DevOps is the key to achieve automation. Some of the reasons for adopting automation are that it enables parallel work to a very large extent, saves time in which multiple other works can be done, is less prone to errors, & saves money, etc.

Automation has established itself in the world completely, everyone is chasing it, there is a huge demand for automation, & that is the reason I have writing this blog to provide a glimpse of the automation world.

Before directly jumping to the practical, let us discuss something about Containerization & Automation tools that are used in the practical.

Brief about Containerization!

It is one of the best technologies ever developed, it enables the industry to successfully deliver the products with low coupling & dependency.

It is a technology in which a single working module is been packed into a container & it is isolated from the rest, this isolation ensures that if there occurs any problem in one container, even then rest all containers are safe, there will be no problem in them.

Brief about Docker!

Docker is a tool which works on the containerization technology & works on OS Level virtualization. Docker shares all the resources with the host machine, & this is one of the reasons Docker is able to launch a complete OS (Docker container) in just a few seconds (generally docker launches a new container is 1 second).

Docker has its own images for the OS it supports, & those images come with limited software pre-installed which makes them very light in size.

Docker container is just an isolated machine running having no knowledge of the outside world.

Brief about Ansible!

Ansible is an automation tool that is used for configuration management. It is a very powerful tool written in python language, it has thousands of modules using which it works, Ansible gets its intelligence from its modules.

Most of the Ansible modules have Intellisense in them, i.e. if the ansible code is executed once again, even then Ansible will first check that whether the operation has been performed or not, if yes, then that part of code will be skipped, otherwise it will be performed.

The program file in which the Ansible code is written is known as Ansible Playbook. To work with Ansible, an Inventory file is been created, by default path is “/etc/ansible/ansible.cfg”. In this path, every IP Address of the machine is mentioned which has to be configured using Ansible.

This much information is enough for getting started, now that being said, let’s start the implementation of the practical.

Ansible-Playbook for the project!

This playbook is been written in a systematic order in which the operations will be performed to accomplish the goal of the task.

Note: This code will work completely fine on the OS based on centOS, for any other OS, the software names have to be changed in the code, & any other tweaks if necessary for the OS. The point here is to understand the flow of the project, then it can be implemented in any of the OS.

Steps to accomplish the Goal:

Note: For every task, check the name tag in the code, it explains the purpose of the code written before it. The code is written in YAML Language, in that language proper indentation is required. For the complete code of the project in one place, check out the link present at the end of this article.

  1. Adding the Yum Repository for Docker
Code to create a Yum Repository for the Docker!

2. Installing the Docker Software and Enabling its services!

Code to Download & Install Docker, also enabling its services!

3. Installing Docker-py Module(For downloading the Docker Images using ansible code)!

Code to Download the Docker-py Module!

4. Downloading the Webserver Docker Image!

Code to Download the Webserver Docker Image!

5. Starting the Firewall!

The firewall has to be in working condition before proceeding with further tasks to accomplish the project goal.

Code to start the firewall!

6. Setting up the Webserver!

Code to setup the Webserver!

7. Creating a Docker Container for Webserver & Disabling the Firewall!

Code to create a docker container for webserver & disabling the firewall!

The firewall is disabled because it is not, then it will not allow anyone outside the docker host to access the webserver. If you know how to add rules for accessing the webserver into the firewall, then you can do that also.

Worker Webserver Glimpse!

After completing the project successfully, when the webserver is been hit, then you will see a webpage as shown below(if you have used the same webpage which I am using, otherwise the webpage will be different).

Image for post
Working Webserver [Image by Author]!

For the complete code on one page with the webpage, check out the link given below.

I hope my article explains each and everything related to the significance of Mean Squared Error with all the deep concepts and Mathematics. Thank you so much for investing your time in reading my blog & boosting your knowledge!

Written by

Big Data Enthusiast, have a demonstrated history of delivering large and complex projects. Interested in working in the field of AI and Data Science.

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