Configuring Docker through Ansible

In this article, we will be configuring Docker using Ansible and after that, we will be performing some more operations.

🤔What is Ansible?

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning. Automation is crucial these days, with IT environments that are too complex and often need to scale too quickly for system administrators and developers to keep up if they had to do everything manually. Automation simplifies complex tasks, not just making developers’ jobs more manageable but allowing them to focus attention on other tasks that add value to an organization. In other words, it frees up time and increases efficiency. And Ansible, as noted above, is rapidly rising to the top in the world of automation tools.

🤔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 should be done in this process.

Configure Docker through ansible.
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 webserver.

Check that the ansible is installed in the system or not.

After that create a file, in this file we will be defining the IP address of the nodes that we want to manage this is known as Inventory.

Start doing the task step by step.

We will be creating a playbook with extension .yml.

🔳 Step-1)

In this step we will be configuring docker for this we will be writing a block of code mentioned below in the playbook.

This code will create a repo and will configure yum in the managed node.

- name:
hosts: all
tasks:
- name: Docker Repo
yum_repository:
name: docker_repo
description: Repo for Docker
baseurl:
https://download.docker.com/linux/centos/7/x86_64/stable
gpgcheck: false

Now, we will install a docker in the managed node.

For installation, we will use the code mentioned below.

- name: docker installation
package:
name: "docker-ce-18.09.1-3.el7.x86_64"
state: present

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 we will start and enable docker services.

- name: Start docker
service:
name: "docker"
state: started
enabled: yes

Now, we will need Docker SDK this is important because Docker SDK is necessary for using the Ansible Docker Module.

For this we will use:

- name: docker sdk
command: pip3 install docker

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:

- name: pull image
docker_image:
name: httpd
source: pull

This code will pull the httpd image from the docker hub. Now, we will be creating a folder in the remote system and then will copy the code from the controller node further mounting that folder to the docker container.

For folder creation:

- name: create a folder
file:
path: "/root/etc/ansible/ansibleplaybook/mycode
state: directory

Now, we will be copying our website from the controller node to the managed node.

- name: copy
copy:
dest: "/var/www/html/"
src: abhi.html

Here I have already created an Html file named as abhi.html. We can also use the content keyword and can write the data directly.

🔳 Step-4)

Now, we will launch one container and will expose it to the public.

- name: launch container
docker_container:
name: httpd_webserver
state: started
exposed_ports: "80"
image: httpd
ports: 8080:80
volumes: /var/www/html/:/usr/local/apache2/htdocs
command: httpd -D FOREGROUND

That’s all we have created and playbook that will configure yum, install docker, copy the code from controller node to managed node, enable the service, and finally created a container that is further exposed publically.

Full code of Playbook:-

After writing the playbook now we will run it by using

ansible-playbook <filename.yml>

Let’s check whether everything goes right or not.

yum has been configured.

Docker has been installed

The container was also launched and exposed.

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

Github Link for Code:

https://github.com/Abhinav1808/Docker_Task.git