Docker Services Using Ansible

SATVIK ARYA
3 min readApr 10, 2021

“🔰 Write an Ansible PlayBook that does the following operations in the managed nodes:

🔹 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.”

Before showing demo, let me tell you why we need such infrastructure? and why we need Ansible, Docker tools?

Story Begins:

jack: Since last month i didn’t change my content on the webserver because whenever i try to change the content on the webserver, client’s request keep on increasing and i have to create replica of my setup on other instances and it requires lot’s of efforts and come up with huge cost which is not good for my personal life. I have to always worried about, that webserver don’t crash, as no. of request increasing. That’s why i want to change my setup, i just want to worry about my content not about client’s request?

Eric: We can create such setup where you have to only worry about your content not for client’s request.

Jack:🙄+😯=>😃, How?

Eric: Recently RedHat introduced tool called Ansible which is an automation tool, used for configuration like setting up webserver and most interesting about ansible is we can integrate it with other tools like Docker, Cloud etc. And it’s a agent less program which means you don’t need to install Ansible on the managed nodes, only you need IP of managed nodes and now many company is adopting ansible for configuration. It is based on imperative language where you have to only tell to ansible “what to do?” their is no need to tell “How to do?”

Jack: How Ansible can resolve my problem?

Eric: Let me explain you, Have you ever heard about Docker product works on container technology. Which is used to launch an OS within one second and ansible is integrated with Docker product so only you have to create Ansible-playbook and behind the scene it will launch OS and then you can setup webserver and for setting up webserver, you don’t have to go manually to setup webserver, Ansible is mainly made for setting up such configuration. Only you have to run code which resides in Ansible-playbook so in that way you don’t have to waste your time for worrying about client request, you can launch as many Docker container as per client request increases.

Jack: 👲, How we can setup?

Eric: I m going to install Docker product, start the service and pull httpd image using Ansible playbook. Then i will run image using Ansible.

Now we have to only copy our content from our controller node to the container in which webserver is already setup. Then we will expose it to the outside world.

Now it’s completed:)

Jack:😀, Thanks for helping, now i have to only run this playbook to setup infrastructure as no. of request increasing.

Eric: If u want to launch multiple docker container then u have to only change the docker container name and it will launch another container.

Jack: okay, Thanks once again.

Thanks to Vimal Daga Guruji and other brother and sister for helping me.

🙏

--

--