Integration of Docker with ansible

Automation using Ansible, Configuring Docker

Samkit Shah

--

I’ve configured Docker using Ansible by writing Ansible playbook. Let’s see how’s it done.

What is Ansible

Ansible is a configuration management tool. It helps in application deployment , task automation and more. We use ansible when you have some sequence of task to performed on different servers or operating system. In today’s world automation is necessary for quick deployment of application or any other things. We can automate chain of events by writing scripts. Some scripting uses iterative language approach in which we have to specify “What to do , and How to do?”. But ansible uses declarative approach where we just have to mention “What to do?” and How’s it gonna perform the tasks is none of our concern.

Where we install ansible that is known as a Controller Node , and we are going to use controller node to configure web servers and Docker on different nodes and that is known as Managed nodes.

Overview

I want to configure Docker , and for that first I have to configure yum and then install docker , start the docker services. To validate if everything works fine , We also have to configure web-server in my case my using apache.

As ansible is written on the top of python so we can use python package manager to install ansible.

pip3 install ansible 

We have to write an ansible playbook for configuration of Docker.

For installation of docker , first we have to configure yum which allows us to automatically install or update packages.

Here I’ve mention in which host I would like to configure yum i.e rhelgui which has some IP Address. To mention this you have to make an inventory so that our controller node comes to know where you wanna perform the tasks.

Now we have to tell our yum where to collect all the docker packages from , so we do that by mentioning.

With the help of these two modules we can download and install docker in our managed nodes.

After installing docker , we have to start our docker services which we perform with the help of service module.

Now my ultimate goal is to build my own docker image and launch a container using that image which has apache web-sever configured.

So I’ve created a basic Dockerfile which fulfills the above criteria

It uses centos as a base OS and if my managed Nodes don’t have centos image , it automatically pulls the image from the docker repository. Also , I’ve installed httpd server and started its services. And for testing I’ve created a html webpage to see if everything works properly.

Copy module copies the Dockerfile and html file which I’ve created to the path mentioned and modules docker_image pulls the centos image that we mention in our dockerfile and perform sequence of commands which we specified.

Finally the last step is to launch a container where we have web -server configured.

docker_container module launches the container from the image webpage which we built using dockerfile. And we exposed it to port number 1234 for testing.

As we have written ansible playbook in yml format so we have to save the file with .yml extension.

There also might be a requirement of docker-py package.

To run ansible-playbook we use the command below:

ansible-playbook docker_config.yml

As we can see we got no red errors and everything seems to configured perfectly. Let’s test it by using the ip address and check if we get the webpage.

IT WORKS!

Left: Managed Node Right:Controller Node

Thanks for reading . I hope you find it useful !

Let’s connect on Linkedln

--

--

Samkit Shah

Machine Learning | Deep Learning | DevOps | MLOps | Cloud Computing | BigData