Configure Apache Webserver on top of Docker Container with help of Ansible Playbook

Let's first understand the basics of the technology that we going to use in this setup so that we can understand it more effectively.

What is Ansible?

What is Ansible Playbook?

It contains the steps which the user wants to execute on a particular machine. And playbooks are run sequentially. Playbooks are the building blocks for all the use cases of Ansible.

Ansible playbooks tend to be more configuration language than a programming language.

What is Docker?

or in a way, we can say that Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, it allows applications to use the same Linux kernel as the system that they’re running on and only requires applications to be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application.

What is Container?

For those coming from virtualized environments, containers are often compared with virtual machines (VMs). We might already be familiar with VMs: a guest operating system such as Linux or Windows runs on top of a host operating system with virtualized access to the underlying hardware. Like virtual machines, containers allow us to package our application together with libraries and other dependencies, providing isolated environments for running our software services.

As we see that below in the image, the similarities end here as containers offer a far more lightweight unit as compare to VMs for developers and IT teams to work with, carrying a lot of benefits.

What is Apache(HTTPD) Web Server?

What is a Web Server?

Now, let's start the practical


  1. Start and enable Docker services.
  2. Pull the httpd server image from the Docker Hub.
  3. Run the httpd container and expose it to the public.
  4. Copy the html code in the/var/www/html directory and start the webserver.

First, we have to set up 2 VM one for Controller Node and the other be the Managed/Target Node. As you see in the below image,

Now, we have to install ansible in our Controlled Node and for this, we use the command: pip3 install ansible

and to check the version of ansible use command: ansible --version

Now, we have to set the target node details and for this, we have to put the information about the target node in one file that is an inventory file.

Initially, If we run the command: ansible all --list-hosts to see a list of all inventory files. we see that there is no inventory.

So, initially first time we have to tell ansible that we have multiple computers/IPs/multiple systems which are running anywhere maybe in the cloud, docker, or on any VM you have to manage these. So for this, we have to create a database in which we have to give the information about the target node and tell ansible that you have to go to these IPs and manage them and in the ansible world this database where a list of all IPs store is known as inventory.

Because ansible knows about its target node from the inventory file so we have to first create the inventory file.

Create Inventory File:

Inventory File contains the following information about Target Node/Managed Node:

  1. IP Address
  2. Which Protocol to use for connection(like ssh)
  3. User Name
  4. Password

Now, first I have created a file with the name ip.txt you can give any name and then put all the above information of TN in it.

Now we have to give the path of our inventory file to the ansible configuration file because whenever we run any ansible command so they first check the Ansible configuration file and if the file exists they go inside and do further things.

So, to check ansible configuration file is exist or not run: ansible --version

we see that file not exist,

So we have to create this file first and for this,

Create Config. File:

By default folder where the configuration file located in /etc/ansible

so, firstly we have to create this folder using: mkdir /etc/ansible

now we put the location of our inventory file in the configuration file using: vim /etc/ansible/ansible.cfg

now if we again see configuration file exist or not we see now it exist.

Now if we run: ansible all --list-hosts we see that our TN IP exists in our inventory file.

Now our controlled node is setup successfully. and we don't need to do anything in the target node.

To check the connectivity i.e the target node is connected to the controlled node or not use the command: ansible all -m ping

So for Configure Apache Webserver on top of Docker Container with help of Ansible :

As we see yum is not configured in our target node so firstly we have to configure yum so that we can install the software.

We can configure yum in the target node manually also but I have configured yum by using ansible-playbook.

Now if we run our playbook in a controlled node:

Now we see that in Target Node our yum is successfully configured

Now we have to check if docker software install or not in our target node and we see docker is not installed in our target node

so again we can install the docker package and start its service on TN using a playbook

and we see that in TN docker is successfully install and running

Now, we have to note one thing that to use an ansible docker module we require the Docker SDK for Python installed

Also, note that only one of docker and docker-py must be installed. Installing both will result in a broken installation. If this happens, Ansible will detect it and inform you about it:

We Cannot have both the docker-py and docker python modules installed together as they use the same namespace and cause a corrupt installation. Please uninstall both packages, and re-install only the docker-py or docker python module. It is recommended to install the docker module if no support
for Python 2.6 is required.

Initially, in our target node, there is no httpd image. so now we have to Pull or download the httpd image from the docker hub so that we can launch or create an httpd container.

so we see that in our TN one httpd image is successfully downloaded.

Now we have to launch or run our httpd server with the help of the image that we downloaded above and then expose it to the public.

Now we see that our container is successfully launched.

now to see more details about the container use command: docker inspect

now we have to copy our html code in /var/www/html folder

now we see that in our target node our index.html is copied successfully

so to see that our web server is started or not use the command: netstat -tnlp

Now if we try to connect with container IP i.e and we see the web page i.e index.html with Output Configuration HTTP server on docker using Ansible Playbook!! which we have written in index.html.

or if we also want that anyone can outside connect to our webpage i.e index.html so for that we have to stop our firewall

Now anyone from outside can connect to our page

So this is our final playbook for this setup:

ThankYou for Reading!!

The Wrap-Up

In the upcoming days I am going to publish lots of articles on different automation Tools and other Technologies, So definitely follow me on Medium.

Here is my LinkedIn profile if u have any queries definitely comment below or DM me on Linkedin

I am tech enthusiast fascinated towards technology and its various disciplines including Big Data, Hadoop, Web Development, Competative Programming,ML,etc.