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.
After that we have to configure ansible.cfg file. This file contains the location of our inventory file ip.txt .
To check the list of hosts :
ansible all --list-host
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
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 !!
We can also check the syntax of the playbook which we have created just now :
ansible-playbook --syntax-check dockerplay.yml
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
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
For checking docker status :
systemctl status docker
For checking images of docker :
docker images
To check whether the container is created :
docker ps
To check the IP Address of the container :
docker exec httpd_server cat /etc/hosts
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
Now let's check our webpage by entering the IP Address with the file name on google :
172.17.0.2/index.html
This is our webpage !!
Thank You...
Ex-SDE @BotLab Dynamics | GSoC'23-24 @ArduPilot
4 年Congratulations