Deploying Docker container using Ansible

Deploying Docker container using Ansible

TASK DESCRIPTION:

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

In this task we are going to integrate the container technology i.e Docker with automation tool ansible .

Docker :

Docker is the most popular file format for Linux-based container development and deployments. If you’re using containers, you’re most likely familiar with the container-specific toolset of Docker tools that enable you to create and deploy container images to a cloud-based container hosting environment.

Automate Docker with Ansible:

Ansible is the way to automate Docker in your environment. Ansible enables you to operationalize your Docker container build and deployment process in ways that you’re likely doing manually today, or not doing at all.

STEPS:

1)First we launch our operating system on which we install the ansible tool and will work as a controller node . Then , we install ansible tool by using python command .

#pip3 install ansible ---> Command used for installing ansible tool .After installing successfully we the configuration of ansible by checking ansible version .

No alt text provided for this image

2)To set the target node for ansible tool we configure one slave node or managed node . Ansible knows about its target node from inventory file that we have to create and give the path of that inventory file to ansible configuration file -- > /etc/ansible/ansible.cfg.

To see all the target nodes or hosts of ansible run the command --> ansible all --list-hosts.

No alt text provided for this image

To check all the nodes are connected or can ping each other run the command --->ansible all -m ping

No alt text provided for this image

3)To install software the main thing is yum configuration .Then first we have to add docker repository to yum repository of target node .

No alt text provided for this image
No alt text provided for this image

4)Then we have to install docker package having stable version using package module and to start and enable the docker services using service module .

No alt text provided for this image
No alt text provided for this image

5)To use ansible docker module we need to install docker SDK for python3.

No alt text provided for this image

6)Before creating the container we have to copy our simple html code into the folder .For these I create one html file and put it in one separate folder .Then copy the html file into the managed node .For these we require file and copy modules .

No alt text provided for this image

7)To create httpd container we have to first download or pull the httpd docker image for container from docker hub .Then to see the content of the html file that we put inside the container in /web:/usr/local/apache2/htdocs and need to expose the container on port 80 .For these task we use docker_container module .

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

8)To run the whole task that we code in ansible playbook ,run the command -->

#ansible-playbook webserver.yml

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

9)Now we are ready to connect the webserver we configured in httpd docker container .Use the <managed_node_ip>:8080 to connect to the webserver .

No alt text provided for this image

We can also connect to webserver through command prompt using curl command .

No alt text provided for this image

______________________________THANKYOU!!!!!!!!!______________________________

To view or add a comment, sign in

Others also viewed

Explore topics