Configure WebApplication in Docker Using Ansible Playbook

Lalita Rajpoot
5 min readApr 29, 2022

Before move forward, we’ll discuss about basics of Docker and ansible.

So Let’s start……

What is Docker?

Docker is container tool that helps developers to package the application in the container in the form of image so anybody can run this application at any environment according to requirement.

Using Docker, you can quickly deploy and scale applications into any environment and know your code will run.

How Docker works

Docker works by providing a standard way to run your code. Docker is an operating system for containers. Similar to how a virtual machine virtualizes server hardware, containers virtualize the operating system of a server. Docker is installed on each server and provides simple commands you can use to build, start, or stop containers.

What is Ansible?

Ansible is configuration management tool written in Python programming language.

In simple words we can say that ansible provides the way where we don’t have need to do task manually it automates our task and develop fast. Ansible doesn’t depend on agent software and has no additional security infrastructure, so it’s easy to deploy.

How Ansible works

In Ansible, there are two categories of computers: the control node and managed nodes. The control node is a computer that runs Ansible. There must be at least one control node, although a backup control node may also exist. A managed node is any device being managed by the control node.

we have some basic knowledge about Docker and Ansble. Now, We’ll install ansible in our controller node…

so Let’s move forward…..

pip3 install ansible

Ansible is written in python, so we can install Ansible using pip3,pip3 is nothing but the python package manager.

You can refer this document to install Ansible on your machine link

After install the ansible we’ll check ansible version using following command.

ansible --version

Now, will go into manage node and check IP of this node to configure in (/etc/ansible/hosts) dir of controller to manage it.

One more important thing to keep in mind before go forward make sure we have configured yum in our manage node

IP of manage node

Setting the manage node configuration file so that ansible system can connect to the target node. Created the host-file which contains all this info of manage node in dir (/etc/ansible/hosts)

Create the ansible.cfg which is configuration file for ansible and store inventory.

Check list of nodes are present in our inventory file use command given below command

ansible all --list-hosts

Now, we’ll check our controller nodes is fully connected to manage node with the help of this command

ansible -m ping all

As we can see that there is message “ping: pong” which means our managed node is successfully connected with our controller node.

Now, we’ll Create a ansible playbook and in this playbook we configure yum repository for docker and installed docker package and through pip we install docker sdk, Also start the docker service.

i’ll create a ansible-docker playbook by using yaml code the name of our playbook is ansibe-docker.yml and webapp.html file is used to copy html code in /var/www/html directory.

Configure Yum repo for Docker

Here we are using yum _repository module to configure yum, here we are not including and gpg key for the package so we just simply giving gpgcheck value as no. This Module will create a repo file docker and include all details we passed in module arguments

Install Docker

Here we are using Package module to do so, state present mean docker should be installed on the managed node

Start and enable Docker services

Pull the httpd server image from the Docker Hub and Run the docker container and expose it to the public

Copy the html code in /var/www/html directory and start the web server

This is a simple playbook to configure Apache Web-Server in Docker Container using Ansible Playbook . This code can be used for each environment if you want to duplicate a similar setup to any other environment; you just need to make sure you have updated the required values in your environment-specific inventory file.

To run ansible playbook we have to write command:

ansible-playbook your_playbook_name.yml
ansible-playbook ansible-docker.yml
Successfully Docker installed in Manage Node

Successfully run our playbook and Docker installed in manage node .

we have httpd image container.

Use command to check IP of this container

docker inspect container_id | grep IP
docker inspect d5180eccb664 | grep IP
so we got the IP 172.17.0.2

Now we can run our webApp over web the browser

172.17.0.2/webapp.html

Thanks for reading my artcle….

--

--