How to Seamlessly Automate Docker using Ansible.

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration.

Docker is a set of platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels. All containers are run by a single operating system kernel and therefore use fewer resources than virtual machines.

Lets perform a task of how to use docker with the ansible playbook, in this playbook we have to configure the docker on the target node and also launch a docker container having httpd image on the same target node using the playbook. Lets first see the task:-



Prequisites for the following task:-

  • You must have the ansible configured in the base os.
  • Also in the inventory file of the ansible must have configured the target node.

Follow the bellow mentioned steps to solve the above task,

Step 1:Create a playbook using the following cmd:-

vim dock_ans.yml

The playbook must have the “.yml” extenstion.

Step 2:Configurethe Docker repository:-

In this step we have to configure the docker repository, for this use the “yum_repository” module of the ansible.



Step 3:Install the Docker:-

For this we have to use the “command” module instead of the “package” module because in the package module we cannot pass the option while installing the software.



Step 4:Start the Docker service:-

For starting the service use the “service” module of the ansible.



Step 5:Copy the data(html page code) to the target node:-

For copying the data we can use the “copy” module of the ansible.



Step 6:Launch a docker container:-

For launching the docker container we can use the “docker_container” module of the ansible. While launching the container we have to attach the volume containing the webpage to the volume of the container that can be access by the outside world , also have to enable the pnatting service for that container.


The above code will automatically will download the httpd image from the docker hub.


Full Code:-

To run the Code use the following command:-

ansible-playbook dock_ans.yml


The web Page that is present on the docker container will be like:-


Ansible is the easiest way to automate Docker in your development environment. Ansible allows you to automate your Docker container build and deployment method in means that you’re likely doing manually. Ansible seamlessly automates Docker and operationalizes the process of building and deploying containers.

Thanks For Reading…….



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store