
š§± Setup Web Server inside Docker Container Using Ansible Playbook
Every Technology is getting automated by Declarative Programming with modules, Plugins and APIs. So as Ansible does, mainly the tool is used for Configuration Management (I have discussed in one of my BLOG).
Our Requirements Change as advancements in Infrastructure of Environment is being done day-by-day from Desktop OS to Virtual OS and Now Containerized OS. The significance of Containerized Environment is increasing in the Organizations for Developing, Testing, Tuning, Replications, Backups and much more.
For Configuring the setup of Docker Engine, pulling image from repository, creating container, Setting Up Apache2 Web Server and coping the web pages then starting Service and Exposing the port where web service is running, We will use Ansible Playbook. Using Docker Community Modules we can do these all at one call of our Automated Playbook.
Letās Start implementing this step by step:
Full Description as follows:
š¹ Configure Docker
š¹ Start and enable Docker service
š¹ Pull the httpd server image from the Docker Hub
š¹ Run the docker container and expose it to the public
š¹ Copy the html code in /var/www/html directory and start the web server
Pre-requisites before implementing the above set of Tasks:
1. Ansible must be configured on your Controller Node.
2. Basic knowledge of YAML
3. Basic Knowledge of Docker Tool
Create a workspace over your Controller Node.
> mkdir /task10
Create a playbook file and letās start writing code for our tasks.
- Docker Configuration over Managed Node (Described in Inventory).
> vim docker.yml
- hosts: localhost
vars:
ā soft : ādocker-ceā
ā url : āhttps://download.docker.com/linux/centos/7/x86_64/stable/"
tasks:
ā name: āCreate docker repoā
yum_repository:
name: docker
description: ādocker community editionā
baseurl: ā{{ url }}ā
gpgcheck: noname: āInstall docker-ceā
command:
cmd: āyum install {{ soft }} -y ā nobestā
2. Start and Enable Docker Service
- name: āStart Docker Serviceā
service:
name: ādockerā
state: started
enabled: yes
3. Install SDK for Docker Tool Management
- name: āinstall docker sdk by pipā
command: pip3 install docker-py
4. Pull the httpd server image from the Docker Hub
- name: āpulling httpd imageā
docker_image:
name: āhttpdā
source: pull
We need to copy our code from SCM i.e. GitHub and that too inside the docker container by mounting volume to the destination path of webserver.
5. Pulling the files over managed nodes from GitHub
- name: ācreating source directoryā
file:
path: /index
state: directory- name: ācopy code from github to directoryā
git:
repo: https://github.com/Uni-wv/task.git
dest: /index
Lastly we have all the three requirements of httpd setup. Now letās create Container using the image we have pulled and then will start the Service.
6. Run the docker container and expose it to the public and Copy the html code in /var/www/html directory and start the web server
- name: ācreating a httpd containerā
docker_container:
name: webcon
image: āhttpdā
volumes:
ā /index:/usr/local/apache2/htdocs
exposed_port:
ā ā81ā
ports:
ā ā8888:80ā
Now Letās Save the code and get it run using ansible-playbook command.


Wow!! My Container got Launched Let me show you the output of ādocker psā command which shows the number of running containers.

Let us see the Web Page running over this container.

We have Successfully completed the Task. Hence in the same manner we can rollup other Webpages too onto our setup. Benefit we get from Ansible is that it works on Idempotence Nature so whenever we change the features of code and then rerun the Ansible-Playbook the Site will update Automatically over all the containers running on our Managed Nodes.
For More details about the Docker Tool and Related Setup using Ansible Visit the Site: https://docs.ansible.com/ansible/latest/scenario_guides/guide_docker.html
Thanks for your Time!! Give Feedback and suggestion to make the tasks more interactive and Simplified.