🧱 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.

  1. 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: no

name: ā€œ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.

Ansible Playbook for our Task
Running The Playbook

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

One Container Running!

Let us see the Web Page running over this container.

Web Page running perfect and get exposed to the base OS port 8888

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.

Technology enhancement take a journey of learning and exploring!! On a way to achieve and Follow my own star!!

Recommended from Medium

Today I Learned — (OK, technically yesterday)

Public cloud cost optimization

Introduction to Data Structures

Acceptance in Data Engineering: Dress Rehearsal

How to Install MySQL 8 on macOS Catalina

Analyzing your google sheets data using Jupyter Notebook server running on your desktop

Managing Semi-Structured Analytics Through Warehouse Tools

Online Shopping Cart System for a Fashion Storeā€Šā€”ā€Špart 4(ratings and comments)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akanksha Singh

Akanksha Singh

Technology enhancement take a journey of learning and exploring!! On a way to achieve and Follow my own star!!

More from Medium

How to version Docker images with GitLab CI/CD

Tutorial for Docker on Windows

Basic Load Balancer using nginx container

Step by Step guide to install Jenkins on Amazon Linux