Configuration Docker container as webserver using ansible

Configuration Docker container as webserver 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 docker container and expose it to the public
  • Copy the html code in /var/www/html directory and start the web server

First we have to install ansible in our controller node through pip3 as is a python module and written in python language.

pip3 install ansible

Then we have to create our inventory file which contains information of all our managed node.

No alt text provided for this image

After that we have to configure ansible.cfg file. This file contains the location of our inventory file ip.txt .

No alt text provided for this image

To check the list of hosts :

ansible all --list-host
No alt text provided for this image

We have controller node with IP Address 192.168.43.67 and managed node with IP Address 192.168.43.137. Let's check the connectivity between two:

ansible all -m ping
No alt text provided for this image

For creating a playbook first we have to know the steps so that we can easily convert those steps into YAML codes in playbook using ansible document.

Now let's create ansible playbook using YAML format: Note : While creating playbook make sure you have proper indentation !!

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

We can also check the syntax of the playbook which we have created just now :

ansible-playbook  --syntax-check  dockerplay.yml
No alt text provided for this image

No syntax error comes that means we can now run our playbook.

For more detailed information we can increase the verbosity by adding -v .

Now let's run the playbook :

ansible-playbook -v dockerplay.yml
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Playbook runs successfully !! Now let's check whether httpd container is running on the top of our managed node.

For checking whether yum has been configured:

yum repolist


No alt text provided for this image

For checking docker status :

systemctl status docker
No alt text provided for this image

For checking images of docker :

docker images
No alt text provided for this image

To check whether the container is created :

docker ps
No alt text provided for this image

To check the IP Address of the container :

docker exec httpd_server cat /etc/hosts

No alt text provided for this image

All the webpages are stored in "/usr/local/apache2/htdocs" in httpd container. To see the content of our webpage :

docker exec httpd_server cat /usr/local/apache2/htdocs/index.html
No alt text provided for this image

Now let's check our webpage by entering the IP Address with the file name on google :

172.17.0.2/index.html
No alt text provided for this image

This is our webpage !!

Thank You...




Asif Khan

Ex-SDE @BotLab Dynamics | GSoC'23-24 @ArduPilot

4 年

Congratulations

要查看或添加评论,请登录

Aakash O.的更多文章

社区洞察

其他会员也浏览了