Configure Docker using Ansible and start the webserver in container

AYUSH BAJPAI
5 min readJan 12, 2021

I have completed Task1 of ansible training given by the Vimal daga sir at linuxworld informatics ltd.

What is Ansible?

Ansible is a configuration management tool that works on declarative language approach.

Ansible is mainly used for configuration management.

Ansible architecture consists of a controller node (where we write code and installed ansible)and any number of O.S which are connected with controller node. these connected computers are known as Target node or managed node.

how to install ansible on controller node ?

# pip3 install ansible

to check that the ansible is successfully installed on your system use following command

# ansible — — version

🔓 What is Docker?

Docker is a software platform for building applications based on containers — small and lightweight execution environments that make shared use of the operating system kernel but otherwise run in isolation from one another. While containers as a concept have been around for some time, Docker, an open source project launched in 2013, helped popularize the technology, and has helped drive the trend towards containerization and microservices in software development that has come to be known as cloud-native development.

👉Following are the steps that we are going to do ,To configure docker using ansible:

🔅 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

firstly check ansible is installed on your system or not using above command.

after that create a inventory file on your system and give it in the configuration file of ansible /etc/ansible/ansible.cfg

Inventory is like a database of IP,in inventory we store ip address ,username ,password ,protocol used .it is used to identify the target nodes.

now start doing task step by step.

we will be creating a playbook with extension .yml.

👍STEP 1)

In this step we will be configuring docker for the system and configuring yum . For this we will be writing the block of code given below:

from the above command we can also do the installation of docker-ce software also.

In this block of code we are telling ansible to install the docker also we have specified the docker community version that is to be installed.

👉 Step 2)

After installing docker we will start and enable the docker services using the below command:

now we will also need docker SDK(software development kit ) because it is very much important for Ansible Docker Module.

For this we will use :

Now, we have successfully installed and started docker in the managed node.

👉 Step 3)

In this step we will be configuring the webserver, for this first we have to pull the httpd image from the docker hub. For this we will be using below-mentioned code:

It will pull the httpd image from the docker hub. Now, we will be creating a folder in the remote system which will act as workspace for us in managed node and then will copy the code from the controller node further mounting that folder to the docker container.

For Folder creation:

path specifies the location in Target node where you want to create folder.

After that we will be copying our website from the controller node to the managed node using below command:

here I have already created a “ayush.html” file in controller node itself .We can also use the content keyword and can write the data directly.

👉STEP 4):

At last we will launch a httpd container and expose it to the public.

That’s all we have created 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. That’s all .hope you liked the task of configuring docker using ansible.

FULL CODE OF PLAYBOOK:

After writing the playbook now we will run it by using

ansible-playbook <filename.yml>

let’s check whether everything is going right or not:

Controller node:https://gist.github.com/ayushbajpai790/564c29932f840967bfd8a14766c9e82d#file-docker-yml

MANAGED NODE:

YUM HAS BEEN CONFIGURED ON MANAGED NODE.

DOCKER INSTALLED ON M.N.

THE CONTAINER IS LAUNCHED AND EXPOSED TO THE PUBLIC .

Also, file has been successfully copied to managed node from controller node.

file has been aceessed by the public.task completed thank you.

github url link:

--

--

AYUSH BAJPAI
0 Followers

ansible learner,coding enthusiast,learning new technology