Launch WebServer on top of Docker using Ansible.

Here we are deploying the webserver on the top of docker using the Ansible technology. Here we are using the RedHat operating system. Before starting procedure here are some keywords -

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.

Docker :-

Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels. Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications.

WebServer :-

The Apache HTTP Server, colloquially called Apache, is a free and open-source cross-platform web server software, released under the terms of Apache License 2.0. Apache is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation. Apache HTTP Server is a free and open-source web server that delivers web content through the internet. It is commonly referred to as Apache and after development, it quickly became the most popular HTTP client on the web.

Now the steps for the deployment :-

Step 1: First we need to install the ansible in redhat operating system. Ansible is written in python language, So we can use pip command to install the Ansible in control node.

pip3 install ansible

Step 2 : After installing is done by using pip command we can verify the installation.

ansible --version

Step 3 : Now at first we need to create a inventory. Inventory is nothing just the IP address , user_name, password of of managed node.

Step 4: If we check the list of hosts it will show nothing and also gives the warning says “No inventory was parsed”. It is becouse we didn’t write the configuration file.

Step 5 : Here we create the ansible directory and configuration file. In following path -

Step 6 : It is the configuration file of ansible. Here we give the path of inventory file and it’s path so that Ansible can parse that file.

Step 7 : And now if we list the host it will show the IP of the managed node.

ansible all --list-host

Step 8 : We can check the connectivity of the controller node with the managed node.

ansible all -m ping 

Step 9 : Now after creating inventory, configuration file, and inventory we are ready to write the code. In Ansible we write the code in YAML language. Extension for that is .yml

Step 10 : Here we do code in YAML :-

- hosts: all
vars:
— repo_mount_path: “/root/software/”
— dest_iso: “/dev/cdrom”
tasks:
— name: create a folder.
file:
path: /root/software
state: directory
mode: ‘0755’
- name: mount ISO file to the created folder.
mount:
src: “{{dest_iso}}”
path: “{{repo_mount_path}}”
state: mounted
fstype: “iso9660”
- name: Mount ISO file permanently.
lineinfile:
dest: /etc/rc.d/rc.local
line: mount /dev/cdrom /root/software/
state: present
create: yes
- name: Make /etc/rc.d/rc.local file executable!!
command: chmod +x /etc/rc.d/rc.local
- name: Adding yum repository.
yum_repository:
name: dvd1
description: YUM AppStream repo
baseurl: “file://{{repo_mount_path}}/AppStream”
enabled: yes
gpgcheck: no
- name: Adding yum repository.
yum_repository:
name: dvd2
description: YUM BaseOS repo
baseurl: “file://{{repo_mount_path}}//BaseOS”
enabled: yes
gpgcheck: no
- name: Adding docker repository.
yum_repository:
name: docker
description: Docker_repository
baseurl: “https://download.docker.com/linux/centos/7/x86_64/stable/"
gpgcheck: no
- name: Install Docker
package:
name: “docker-ce-18.06.3.ce-3.el7.x86_64”
state: present
- name: Start Docker services.
service:
name: “docker”
state: started
- pip:
name: docker
- name: pull the httpd webserver image from docker hub.
docker_image:
name: “httpd”
source: pull
- name: copying html file from configuration node to managed node.
copy:
src: “/root/task-10/web/index.html”
dest: “/home”
- name: creating a container from downloaded image.
docker_container:
name: web_server
image: httpd
state: started
ports:
— “8080”
volumes:
— /home:/usr/local/apache2/htdocs/

Step 11 : Here are some of the screenshots of the managed node before running the code from the controller node.

Here we have the rc.local file which runs always very after we boot the system. So all the command inside that file always run as soon as we boot the system it is very useful.

We also don’t have docker installed.

Step 12 : We can check the syntax of the code written, if we code have any syntax error then it will show error.

ansible-playbook --syntax-check file_name.yml

Step 13 : After checking the syntax of the code and is code doesn’t give any syntax error we are ready to run the code on controller node.

ansible-playbook file_name.yml

Here are the screenshots while running the code :-

Our code doesn’t give any error, So we are good till now.

Step 14 : Here are some screenshots of managed node after running the code.

  • Here the folder is created and ISO file is mounted on this folder.
  • Here the command for mounting the ISO file to the software folder is written in rc.local file so every time the system boot it will automatically mount the folder as this file run always as soon as we boot the system.
  • YUM is configured
  • Docker is successfully installed
  • Docker image is downloaded. (Here we download httpd image)

Step 15 : Now we check if there is any docker container is running.

docker ps

Here the name of the container is web_server.

Step 16 : Here we go to the container and ask for terminal. We have htdocs folder where we have html code we already started the webservices so we can directly see the webpage. Here we can see our code which is copied from the controller node.

Step 17 : Here is the output of the code which is deployed on the top of docker.

Conclusion :

Here we use Ansible to automate the whole configuration process, We use docker to launch the webserver on top of it. We used httpd webserver for hosting the website. The port number for httpd webserver on docker is 80 and our base os ie. RedHat route the traffic via it’s own port number that we have set ie. 8080. So in this way we successfully launched the website and achieve the automation!!

A computer Engineer Student

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