Gaurav Tank

Mar 12, 2021

4 min read

How to configure Docker using Ansible

In this blog, I am going to show you how to configure Docker container and deploy the web services. First of all, let me answer some questions like what is ansible and what is docker

What is Ansible?

Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.

It uses no agents and no additional custom security infrastructure, so it’s easy to deploy — and most importantly, it uses a very simple language (YAML, in the form of Ansible Playbooks) that allow you to describe your automation jobs in a way that approaches plain English.

What is docker?

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

Now, I am going to walk you through the whole process:

Step1: Update your inventory. In this case, I am running this playbook in the localhost.

- hosts: localhost

Step 2: Update your yum repo to download the docker-ce software and install all the dependencies like installing docker library using pip3 cmd.

Here, to provide the idempotency to the shell command I have first checked for the pre-installed docker-ce software If the software is not pre-installed it will install it.

Step 3: is that we have to start the docker services using the service module of ansible.

Step 4: Now we have to pull the docker image that we want to install. Here, I am going to pull the httpd image.

To make this code more dynamic I have used the variables and mentioned it in the vars module.

Step5: Now we have to create a container of httpd image and with the additional -p or port option. This will do the PATing for us so that the DNATing is possible in the container.

Step 6: Finally we have to copy the web pages and start the httpd services.

Congrats! Now you have launched a Docker container with the httpd services running using Ansible playbook. Now, I have attached some screenshots of the ansible-playbook for your reference.

Here, I have already installed docker-ce so it didn’t install it and skipped that part. And httpd image was not available in my OS so it pulled it.

Here, we can see that the httpd image has been pulled and the os named docker_os1 is launched.

It works fine. We configured the docker container using Ansible.

Hope you find this blog informative

Thanks for reading!!