Configuring Docker Containers Using Ansible :

ARTH TASK 10:


task-10.png

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

TASK SOLUTION:


At first before installing and configuring webserver on the managed node ansible first needs to know which node it should manage for this we need to configure ansible for this we can either use the ~/.ansible.cfg(home directory) or ./ansible.cfg(in the directory where we are working) in the configuration file we mention where is our inventory file and we use that inventory file to know the nodes here is the ansible configuration file for the current working directory

[defaults]
inventory=./hosts
remote_user=root
host_key_checking=false

after creating the configuration file we need to create the inventory as mentioned in the configuration file the inventory is at “./hosts” hence we need to create a file hosts in the current directory and put the following content in it

[<host_name>]
<ip_address> ansible_password="<host_password>"

where <host_name> is to label all the hosts in that group, <ip_address> is the IP address of the host on which u want to install webserver on a docker container, and <host_password> is the password of the user mentioned in the ansible configuration file as we need to do configuration we mostly need root permissions hence in ansible configuration file I mentioned remote_user as root

After doing above all steps we can install docker and then we can start an container and install webserver in it

Configuring Docker:

Firstly we need to install docker in the managed node the following is the part of playbook which I used to install docker. I am taking the managed node as a RedHat machine hence for installing docker we need to configure yum. After configuring the yum we need to install docker. Following is the part of ansible playbook which will configure yum and install docker

   - name : "Configuring yum to install docker"
     yum_repository :
      name : "docker"
      description : "docker repository"
      file : "docker-ce"
      base_url : "https://download.docker.com/linux/centos/8/x86_64/stable/"
   - name : "Installing docker"
     shell : "yum install docker-ce --nobest --skip-broken"

Starting docker services:

After installing docker we need to start the docker services to start or run a container. Following task will start the container services.

   - name : "Starting docker services"
     service :
       name : "docker"
       state : started

Pulling httpd image from dockerhub:

After starting the docker services we need to pull the httpd image from the dockerhub using docker_image module of ansible

   - name : "Pulling httpd image from dockerhub"
     docker_image :
       name : "httpd"
       source : "pull"
       state : present

Starting docker container with httpd:

Starting docker container by exposing the port 80 as http service will run on port 80 as we need a port which is not used by anyother service on the host machine for this I have given a port range 8000–8100(any port which is free in this range will be choosen by docker). Following is part of playbook which does this

   - name : "Starting a container with the httpd image"
     docker_container :
       network_mode : "bridge"
       name : "webserver" 
       image : "httpd"
       exposed_ports :
         - "80"
       ports :
         - "8000-8100:80"
       state : started
     register : dockerContainer

Copying the webpages from “content” dirctory to the root document of the httpd docker image:

As the content might contain different types of files we need to copy all files and folders from the content directory to the root document for this I used shell to copy all files from the directory to the container using docker cp command by running through all files and folders in content directory following is part of playbook which will copy the files to the docker container

   - shell : "for f in `ls content`; do docker cp content/$f webserver:/usr/local/apache2/htdocs;done;"

With this we can configure a webserver on docker with the help of ansible

The complete configuration file can be found here: github repo