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

10 min readMar 21, 2021


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?

Ansible is an open-source IT Configuration Management, Deployment & Orchestration tool. It aims to provide large productivity gains to a wide variety of automation challenges. .It runs on many Unix-like systems and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration. Ansible was written by Michael DeHaan and acquired by Red Hat in 2015. Ansible is agentless, temporarily connecting remotely via SSH or Windows Remote Management (allowing remote PowerShell execution) to do its tasks. This tool is very simple to use yet powerful enough to automate complex multi-tier IT application environments.

What is Ansible Playbook?

Playbooks are a configuration file in Ansible which is written in YAML format. It is one of the core features of Ansible which tells Ansible what to execute, and it is used in complex scenarios. They offer increased flexibility. It is a human-readable data serialization language. It is commonly used for configuration files. It can also be used in many applications where data is being stored.

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?

Docker is an open platform for developing, shipping, and running applications by using containers. Docker enables us to separate our applications from our infrastructure so we can deliver software quickly. With Docker, we can manage our infrastructure in the same ways as we manage our applications. So, by taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, we can significantly reduce the delay between writing code and running it in production.

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?

Containers offer a logical packaging mechanism in which applications can be abstracted from the environment in which they actually run. This decoupling allows container-based applications to be deployed easily and consistently, regardless of whether the target environment is a private data center, the public cloud, or even a developer’s personal laptop. Containerization provides a clean separation of concerns, as developers focus on their application logic and dependencies.

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?

Apache HTTPD is one of the most used web servers on the Internet. Apache HTTP Server is a free software/open-source web server for Unix-like systems and other operating systems.

What is a Web Server?

A web server is server software or hardware dedicated to running this software, that can satisfy client requests on the World Wide Web. A web server can, in general, contain one or more websites. A web server processes incoming network requests over HTTP and several other related protocols.

Now, let's start the practical


  1. Configure Docker.
  2. Start and enable Docker services.
  3. Pull the httpd server image from the Docker Hub.
  4. Run the httpd container and expose it to the public.
  5. 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

I have successfully Configuring (Apache)HTTPD Server on top of Docker Container using Ansible Playbook.

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.