🧱 Setup Web Server inside Docker Container Using Ansible Playbook

Akanksha Singh
4 min readJan 22, 2021

--

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.

--

--

Akanksha Singh

Platform Engineer | Kubernetes | Docker | Terraform | Helm | AWS | Azure | Groovy | Jenkins | Git, GitHub | Sonar | NMAP and other Scan and Monitoring tool