How to Seamlessly Automate Docker using Ansible.

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:-

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:-

Task:-

🔰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

Solution:-

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.

Code:-

Output:-

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.

Code:-

Output:-

Step 4:Start the Docker service:-

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

Code:-

Output:-

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.

Code:-

Output:-

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.

Code:-

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

Output:-

Full Code:-

To run the Code use the following command:-

ansible-playbook dock_ans.yml

Output:-

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

Conclusion:-

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…….

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What exactly is ‘Momentum’ in SGD with Momentum?

TripGo API — Special features

Building Stevie the Robot’s​ AI

Web Scrapping using Python

Getting Started with Docker

iOS 13 — Sign In with Apple ID

2019: The Year Bee-hind Us — Honeycomb

Lit sparkler being held in front of a person.

5 Useful Non Technical Skills when you Hire iPhone App Developers

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
Gaurav Khore

Gaurav Khore

More from Medium

How to install Apache Web Server on Ubuntu Linux

A complete Prometheus based email monitoring system using docker compose

Create a simple Node.js Hello World Docker Container from scratch

Create Docker Image Using Docker File and Push It On Docker Hub