Webserver Configuration using Ansible and Docker

In this blog, we would :

  • Configure and start Docker Services.
  • Pull apache httpd server image from DockerHub.
  • Run the docker container and expose it to public.
  • Copy the html code in document root .
  • Start the webserver.

And all the above steps would be performed on a separate remote system (or a VM) using Ansible.

Refer to my blog (given below) if you want to know more about what Ansible is and how it is used by the IT industry.

Ansible Architecture:

The Ansible architecture broadly consists of the following:

Control Node: Often known as the brain of any Ansible configuration, it is the device where Ansible is installed and is used by the user to control the managed nodes. Control node is the place where ansible plays are written by the user.

Managed Nodes: These are the devices/servers that are managed by Ansible. All the tasks are performed on these nodes.

Playbook: Ansible playbook contains a list of tasks that are performed on the managed nodes. It can be categorized as a type of “script”, which contains a number of plays. It is written in YAML format.

Inventory: It is a user created file which contains the details of the target/managed nodes. Details like IP address, username, password, connection type etc. are stored in this file. This file is responsible for establishing a successful connection between the control and managed nodes in order to perform any task.

Docker:

In simple terms, Docker is a software platform used for building applications based on containers — small and lightweight execution environments that make shared use of the operating system kernel but otherwise run in isolation from one another. Containers are isolated from one another and bundle their own software, libraries and configuration files. All containers are run by a single operating system kernel and therefore use fewer resources than virtual machines.

That’s all the basics you need to know to dive into this task. So lets get started!

Step 1: Install Ansible in your local system/controller node. Here I am using Windows Subsystem for Linux as a local system. This provides a Linux environment inside a Windows machine without the need of running a virtual machine. The following commands would install and check the version of Ansible in your system. You would also need to install sshpass as it is a dependancy of Linux.

#to install Ansible
pip3 install ansible
#to install sshpass
yum install sshpass
#to check the version of Ansible installed in the system
ansible --version
output

Step 2: Create an inventory file (text file) and add the details of your managed nodes in the format given below:

<ip address> ansible_user=root ansible_ssh_pass=<password> ansible_connection=ssh

Now go to ansible configuration file and provide the location of your inventory file.

#to open ansible configuration file
vim /etc/ansible/ansible.conf

Note: Create a folder at “/etc/ansible/” if not already created and follow the above step.

Ansible Configuration file

Step 3: Now that you’ve configured Ansible, check whether your managed nodes are connected and active.

#to check the list of managed nodes
ansible all --list-hosts
#to check the connectivity
ansible all -m ping
Connectivity

Step 4: Now that we’ve successfully installed, configured and tested the connectivity, we can start writing the playbook for the tasks mentioned at the beginning of the blog.

#to create a playbook
vim docker_task.yml

Playbook contents:

Playbook Configuration

Step 5: Running the playbook:

ansible-playbook docker_task.yml
Playbook execution output

In the above picture, we can see that the playbook has been successfully executed and no error is encountered during the process.

We can do the following steps to verify that our task has been successfully executed:

  1. Docker configuration status in managed node:
systemctl status docker
Status of Docker services in managed node

2. Check the status of httpd image pulled in managed node:

docker images
Docker images

3. Check whether the container is launched or not:

docker ps
list of active containers

4. Check the deployment status of webserver launched in the container. We can access the webpage in two ways:

  1. Using the the IP address of the container.
#command to find the IP of container
docker inspect webserver1 | grep IP
here the IP address is 172.17.0.2

2. Since we’ve used port forwarding, we can access the same webpage on the host machine (where container is running) using its IP address along with the port which is forwarded. Go to your browser and type this in the URL:

http://<ip address>:8080/index.html

Deployment status:

Final webpage accessed using Mozilla Firefox using Container IP address

Link to the source code:

--

--

Final Year Undergrad from Indian Institute of Information Technology Ranchi interested in learning the ins and outs of Technology

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
Akshansh Singh

Final Year Undergrad from Indian Institute of Information Technology Ranchi interested in learning the ins and outs of Technology