Integration of Docker and Ansible

Rahul Jain
3 min readMar 21, 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 httpd container and expose it to the public

🔹 Copy the html code in /var/www/html directory and

start the web server

INTRODUCTION TO ANSIBLE -

*Ansible is tool which is used for Configuration Management (CM). In Ansible we only tell to “what to do”. We don’t need to tell “How to do” because Ansible already knows that “How to do this task” on each type of Operating System .

*This intelligence of Ansible comes from “modules” . Modules know “How to do this operation on each OS”. We don’t require to tell particular OS command. Ansible don’t perform any operation on OS. It is done by respective OS command. Ansible module know which command is required to run on OS to do this.

INTRODUCTION TO DOCKER -

*To setup operating system we have four technologies -

1). Bare Metal

2). Virtualization

3). Cloud

4). Containerization

*In Bare Metal , Virtualization and Cloud technologies operating system takes 1/2 hour to setup OS. If any reason OS is crashed then it will again take same time to setup this. But in Containerization technology OS takes less than only some seconds to setup this. It is very helpful in many cases.

*Docker is tool which works on containerization technology. Docker takes less than 1 second to set up all environment of OS. Docker is very fast to handle this . One of the reason is docker use base OS components.

Prerequisite -

Install two VM’s in the system . One as Controller Node and other as Managed Node and download ansible in Controller Node .

*Now follow these steps in the Controller Node -

1- Check the version of the ansible installed in Controller node with the command “ansible — version”

2- Check the inventory file.

3- Check the configuration file of Ansible.

4. Now, Ansible is installed and configured successfully. We now check the list of inventories:

5.To check the connection with the managed node, we use: “ansible all -m ping”

6. Create a docker.yml file for playbook

7. In docker.yml follow these modules and commands -

8.Run the AnsiblePlaybook by using the command-

Now, the container is successfully created and configured as the webserver. Let’s access the webpage… The IP: 192.168.1.43 and port: 80 “IP Address:Port/filename “

The webserver is working properly.

We can now see the managed node whether the desired services are successfully started or not. In the managed node, we find:

The container is running properly as desired.

--

--