Launching Web Server on a Docker Container Using Ansible
Automating the process of Launching a Docker container with Web Server in Managed Nodes using Ansible
Hey Everyone, Welcome to the blog.
I hope you are here to get some ideas on Ansible and Automation. So let’s get started…
Let me start by explaining Ansible. In short, the word you need to understand that Ansible is a powerful automation tool for configuration. We have to download and set 100 and 1000 systems servers at the industry level to provide smooth service. Configuring one by one and manually is not a good way. Hence, we have some tool by which we need to write some predefined code as per our need and run it one system, and the whole cluster of systems gets configured. Ansible is among one of those tools. You can check me for another blog for getting more information about Ansible and its use case in Industry.
Let's get started with the task now. Ansible is all about automation. We first need to install Ansible in our system. The system with the Ansible software will be the main system or controller node. Another system that is controller by the controller node will be our host systems.
Here the practice is done in a Linux System. As Ansible is purely made with Python, we need to have Python in our system. To install Ansible, we can use the command:
#pip3 install ansible
As we have ansible in the system, we have to inform Ansible about the host systems it needs to work with. So we create a database to store that information.
# mkdir /etc/ansible
# cd /etc/ansible
# vim inventory.txt
We have created a directory inside the “/etc.” directory name as “ansible.” We created a file “inventory.txt” inside that directory.
Now inside, we need t state the host and their authentication details.
# vim inventory.txt
We need to fill in the info in the file. The first one is the host system IP; the second is the connection protocol we are doing that is “ssh”, followed by the system's ID and password. Now we have two hosts connected with our ansible controller node.
Now we have created a database. Now we need to inform Ansible about this database file. So we create a configuration file.
# cd /etc/ansible
# vim ansible.cfg
Now this will create our configuration file, and we will be adding our inventory details. We can add many more things related to Ansible like ansible vault details, variables file, and many more.
For now, we have specified the inventory file in the configuration. As I have both of the files in the same location, I haven’t added the path. We can also create inventory in other locations and add the path here.
Now let’s start to check if ansible is connected or not with the host. We can list our host using the “Ad-Hoc” command. To check the connection, we can do ping pong. It is the way we check the connection with other host systems. During this, the other systems reply to the controller node.
This was the first time configuration with Ansible. Now, as many hosts, we have that many IPs we can write in the inventory file.
Now we move to our task where we will be using Ansible to:
- Mount DVD in Hosts
- Configure YUM and Docker Repositories in Hosts
- Install Docker and Start Docker in Hosts
- Launch a container with HTTP Image
- Send the code file from the Host to the Docker Container
- Start Web-Service from the Container Running in Host
We are going to be doing the task in two-part. We have created two “yml” playbook files for the task.
You can see the code file from GitHub. These are the files that I will be running in my Controller Nodes.
We run the first playbook name as “docker.yml.”
# ansible-playbook docker.yml
All our tasks ran successfully. Now we will check if these tasks are done in our worker node.
We can see in the above image that our YUM repo has been configured in both systems, and docker is also running in both systems.
We ran another playbook named “webserver.yml” where the second part of the task is coded.
We can see that this playbook also ran with success. Let's check the host if the container is launched and the webserver is running or not.
Here in the above image, we can see that the container is started in the host system as we can see from the “docker ps” command, and when we went inside that container and check the “htdocs” directory (THIS IS THE APACHE WEB SERVER HTTPD in UBUNTU). There we can see our “code.html” file.
It is good to use “index.html” as the file name.
Now here in the above image, we can see the information of the container with the command:
# docker inspect <containerID>
We can see the container's IP, and as in our code, we have exposed our webserver with 80 ports. We can connect it from our host system. And we can see in the image when I used the “curl” command; it showed me the content.
# curl http://172.17.0.2/code.html
We can see we have successfully done our task here. We have mounted our host with DVD, configure YUM in them, and then installed and ran docker. We also launched the docker container with the “httpd” image and ran our web server there.
You can also find the YAML code for the playbook in my GitHub.
You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…
I hope I have cleared all the doubts and presented my point clear. If you have any doubt and confusion, always feel free to contact me on my LinkedIn.
Mishan Regmi - Research Intern - SkillGeek | LinkedIn
View Mishan Regmi's profile on LinkedIn, the world's largest professional community. Mishan has 1 job listed on their…
Thank You for your time. Have a Good Day.