Web Server in Docker Container using Ansible

Priyanshu Bhatt
3 min readSep 18, 2023


We all know how Ansible has changed the IT workflow game, The use case of Anbile to configure heavy-duty tasks in multiple servers is what makes today's IT world efficient and consistent. In this blog, we’ll see how we can use Ansible to configure docker services and launch a web page,


1. Apache Webserver:-

  • Apache is an open-source and free web server software that powers around 40% of websites around the world. The official name is Apache HTTP Server is an open-source and free web server software that powers around 40% of websites around the world. The official name is Apache HTTP Server, and it’s maintained and developed by the Apache Software Foundation.
  • It allows website owners to serve content on the web — hence the name “web server.” It’s one of the oldest and most reliable web servers.
  • When someone wants to visit a website, they enter a domain name into the address bar of their browser. Then, the web server delivers the requested files by acting as a virtual delivery man.

1. Docker:-

Docker is an open-source containerization platform. Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.

2. Ansible:-

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. 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 .


  1. A controller node and one Managed node is configured. In the controller node, ansible is pre-installed.
  2. Now first let’s create an inventory file. Where all the information about the target/Managed node will be stored. In the inventory file, we can store multiple IPs. Here I create one redhat 8 cli os as managed node .

Inventory file

Ansible-configuration file

Here host_key_checking=false [ used to auto accept ssh key by Managed Node]

command_warnings=False [disable command warnings]

deprecation_warnings=False [ to disable deprecation warnings]

Let’ Started

First we can verify that our managed node is reachable or not

ansible all -m ping

Our system is working fine.

Now I create a playbook using ansible module to configure webserver on the top of docker

  • I Created a docker container web3 and use Image httpd. The container will run in background by detach option.
  • The container is exposed at port 80 on which webserver is running and forward container port 80 to base redhat 8 port 8080 .
  • We cannot directly login to container . So I mount base directory /root/WS of redhat 8 to container directory /usr/local/apache2/htdocs ( location of webpages of httpd image) and directly copy webpage from controller node to managed node .

We can run playbook by

ansible-playbook docker.yml

After successful execution of playbook , paste Managed node IP: port ( in the browser ,you can see our webpages.

Note :- If you are unable to connect , then it may be firewall issue. So disable the firewall by command “systemctl stop firewalld”



Priyanshu Bhatt

AWS Solutions Architect || Terraform Certified Associate || DevOps Engineer || I Share Crisp Tech Stories