Configuration of Web Server in Docker using Ansible
By: Pritee Dharme .
Tags: LinkedIn, Ansible, Docker, Apache, Web Server

Configuration of Web Server in Docker using Ansible

In this article we are going to see how we can configure the web-server in docker using ansible. Now first we are going to see that what is ansible , docker and web-server.

Ansible :

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning.

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.

Apache HTTP Server :

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.

What we do in this article….??

Configure Docker Start and enable Docker services 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

Now Let’s start our task ..

First we have to install the ansible software . But this software is based on python so we have to first install python3 and in that pip3 is already installed so using following command of pip use install ansible software for doing further task.

“ pip3 install ansible”

In my case it is already installed . Just for conformation we check the version of ansible.

Now my ansible is installed. But for ssh login we have to install one more software that is sshpass.

“ yum install sshpass -y ”

Now , let’s configure the ansible for that first we have to create one folder by giving name “/etc/ansible” or directory in which we configure ansible. And after creating this directory create one file having name ansible and give extension .cfg .

Now we have to configure the inventory file . Inventory file is the where we give all information about target node. Ansible works against multiple managed nodes or “hosts” in your infrastructure at the same time, using a list or group of lists known as inventory.

Inventory file we can create anywhere (Default Locaion is /etc/ansible/hosts)in the Controller Node (The system in which ansible is installed ), and it will mainly consist the few things about Target Node.

IP address of target node

User name which is used to connect( ansible_ssh_user )

) Password of that user( ansible_ssh_pass )

) And which connection we use (local/ssh)(ansible_connection)

Now , in the configuration file of ansible we have to give the path of our inventory file in my case it was “/etc/ansible/myip.txt”.

And in that file we also give some extra options for connection.

Now our ansible is configured . And for that we should Check the list of hosts present in inventory file.

“ ansible all — list-hosts ”

Now , here we see that our ansible is configured successfully. And in that we have 1 target IP. For seeing that our manage node is connected to target node or not.. for that just do pinging.

Here we have connection between target node and manage node.

Now our main step is to write a playbook . So for that we have to create one folder name as workspace and in that create one file having extension .yml. Here ansible support YAML language to write a playbook. YAML is a human-readable data-serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted. That’s why we give extension .yml to the playbook.

Now, in that playbook we configure following things..

Create yum repository to install docker-ce because we have not proper support of docker-ce in RHEL-8, so we are using CentOS online repository to download the software. Now install docker. start docker service and enable it. Install python3 package because we need to install docker py via pip3 in host system to launch docker container. Now pull httpd image from docker hub . Now copy all the web pages, which we want to configure in web server inside the docker image from controller node to target node. Here target node will be Docker host on which container is running. Now launch docker container with having httpd server configured .expose the port 80 because on this port in container web server is running , and use volume module to link folder in which we earlier copied the web pages (In my case it was /root/web/)in target node to document root of web server (i.e. /usr/local/apache2/htdocs/ in httpd image.).

Next, we want to create a web server inside the docker container that should be accessed from outside world but docker container is isolated from outside environment , The network connectivity is can be possible between docker container and docker host , so we have to use a concept port forwarding , so that if we access any port on docker host , it will be redirected to the port 80 of docker container on which web server is running. This creates a firewall rule which maps the container port (i.e. 80)to a port on the Docker host (i.e. 8082 in my case, you can use any in the port range)to the outside world.

Now just run the above playbook.

“ansible-playbook (playbook name)”

Every step is running successfully. Now let’s check the target node.

Now let’s check whether our docker yum is configured or not.

Here our docker yum is configured successfully .. Now let check our docker software is installed or not.And docker service is running or not.

Let’s check our container running or not and our image is pull successfully or not.

Now take the IP of that running container using command..

“ docker inspect httpd | grep IP ”

Here is our IP now take this IP and check our page by giving this to browser.

Yes…. Our Web server is successfully configured in docker using ansible.

I hope this article help you…

****If you like it then Clap and Share….!!!!

*If you like it then follow me in LinkedIn ………

And For any suggestion feel free to connect me in LinkedIn..

***Thank You Everyone For visiting a Blog……!!!!