The Wayback Machine - https://web.archive.org/web/20240712193637/https://www.geeksforgeeks.org/automating-docker-deployments-with-ansible/
Open In App

Automating Docker Deployments With Ansible

Last Updated : 21 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Docker is a tool used to dockerize the application along with its dependencies, on the other hand, Ansible is a tool used to manage configuration and deploy applications on other machines. Here in this guide, I will first discuss what is Docker. Then I will discuss What is Ansible. After this, I will walk you through the steps to deploy and run a nginx docker container by using Ansible.

What Is Docker?

Docker is a containerization tool that helps in packaging the application and its dependency into compact units called docker containers. These docker containers contain all the application code, runtime, libraries, and other dependencies for running an application. Developers first write a Dockerfile mentioning all the base image, working directory, commands to download dependencies, and commands to run the application. Then the Dockerfile is built using the docker build command to create a docker image. Using this docker image developers can run their application on any system, the only condition is that docker should be installed on that system. The docker containers use very less resources of a system. Due to this reason, multiple docker containers can be run in a single machine, which helps in maximization of resource usage of a system and also helps in reducing the overall infrastructure cost to deploy and run an application. Overall we can say docker has become a very crucial tool for the organizations to deploy and run an application on any machine without facing any hardware related issues .

What Is Ansible ?

Ansible is an open source automation tool which helps in configuration management and application deployment . Ansible uses SSH to connect with the other hosts for running tasks . Ansible uses playbook to write the tasks . Tasks are basically the description of a job. For example installation of docker , pulling docker image , etc . Playbooks are written in using YAML . Ansible helps to maintain multiple machine at a time . Lets say there are 10 machines which needs system update . The system update can be performed through Ansible without any manual installation on each machine . This agentless approach reduces the manual overhead . In summary we can say Ansible is a powerful open source automation tool which is used by organizations to manage configuration remotely over multiple machines which results in improving the efficiency and reducing the manual overhead .

A Step-By-Step To Deploy Docker Containers By Using Ansible

Step 1 : Create a master node . Here open SSH port and also use a master key .

Step 2 : Create a worker node . Here open SSH and HTTP port , and also use a worker key .

Step 3 : Connect master node using SSH and install ansible on this .

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible

install-ansible

Step 4 : Now copy the private key of worker node into the master node .

scp -i "Ansible-master.pem" Ansible-worker.pem ubuntu@ec2-54-146-224-195.compute-1.amazonaws.com:

scp-copy

Step 5 : Now make some changes in the ansible configuration file . Here assign host_key_checking to False .

sudo su
ansible-config init --disabled -t all > ansible.cfg
vi ansible.cfg

host_key_checking disabling

Step 6 : Grant the read permission to the private key of worker node that is present inside the master node .

chmod 400 Ansible-worker.pem

chmod

Step 7 : Now create an Inventoryfile . Here mention the private IP of worker node , machine name and the private key .

all:
hosts:
web01:
ansible_host: <Private IP Of Worker Node>
ansible_user: ubuntu
ansible_ssh_private_key_file: <Key Name>

Inventoryfile

Step 8 : Create an ansible playbook which will install docker on the worker node and run a nginx container on port 80.

---
- name: Deploy NGINX Container Using Ansible
hosts: web01
become: yes
tasks:
- name: Update The System
apt:
update_cache: yes
become: yes

- name: Prerequisites Packages Installation
apt:
name: "{{ item }}"
state: present
become: yes
loop:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common

- name: Add The GPG Key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
become: yes

- name: Add The Docker Repository
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable
state: present
become: yes

- name: Docker Installation
apt:
name: docker-ce
state: present
become: yes

- name: Docker service Start
service:
name: docker
state: started
become: yes

- name: Pull The Latest NGINX Docker image
docker_image:
name: nginx
tag: latest
source: pull
become: yes

- name: Run NGINX Container
docker_container:
name: nginx_container
image: nginx
state: started
restart_policy: always
ports:
- "80:80"
become: yes

Install and configure nginx server

Step 9 : Now run the ansible playbook .

ansible-playbook -i Inventoryfile nginx-docker-run.yml

playbook-run

Step 10 : After this connect the public IP of worker node with port 80 on the browser .

nginx

Conclusion

Here in this article you have first learned what is Docker and how it helps run an application easily . Then you have learned what is Ansible . After this you have created a master node where you have installed ansible and also made some changes in ansible configuration . You also created a worker node on which you have run a nginx docker container using ansible playbook on master node .

Automating Docker Deployments With Ansible – FAQ’s

What Is The Difference Between Ansible And Terraform ?

Terraform is mostly used for provisioning infrastructure on cloud platform while Ansible mostly helps in managing configuration of multiple machine.

Which Ansible Configuration Need To Change For Complete Automation ?

In Ansible configuration file you need change the host_key_checking to False . This configuration will allow the automatic execution ansible playbooks without asking any type of questions .

What Is The Ssh Port Number ?

22 is the SSH port number .

What Is Playbook In Ansible ?

Ansible playbooks are the files where a set of tasks are mentioned for execution on the remote machines .

What Are The Advantages Of Using Ansible ?

Using Ansible decreases the manual overhead and improves overall efficiency .



Similar Reads

Blue-Green Deployments with Docker Swarm
Maintaining operational continuity and user satisfaction with minimal downtime is critical in today's fast-paced digital ecosystem. This article delves into blue-green deployment, an effective strategy designed for application updates with zero downtime. Tailored for DevOps professionals and system administrators, this article specifically focuses
15+ min read
Ansible vs Ansible Core
The landscape of DevOps and automation is highly dynamic, with Ansible being among the tools that have been developed for configuration management, application deployment and IT orchestration. Nonetheless, within the realm of Ansible, there are two distinct entities called “Ansible” and “Ansible Core”, each having its own weightage and functionalit
9 min read
How To Install Docker Using Ansible Playbook ?
Docker is a software platform that allows you to build, test and deploy applications that use OS-Level virtualization to deliver software in packages called "containers". Containers - Docker package software into standardized units called "Containers". Docker is a stand-alone tool. which means no need to install run-time. The manner in which we mak
7 min read
Kubernetes Deployments - Security Best Practices
Pre-requisite: Kubernetes Applications can be more easily managed by Kubernetes, which automates operational tasks of container management and includes built-in commands for deploying applications, rolling out changes to your applications, scaling your applications up and down to fit changing needs, monitoring your applications, and more. Due to it
5 min read
AWS Lambda Deployments with AWS CloudFormation
AWS CloudFormation allows to create AWS resources with help of templates. It provisions and deploy all the resources required for the entire application described as a Stack. Templates can be updated and replicated as required to reduce the overhead of implementing and provisioning infrastructure. let's learn how we can provision lambda deployments
6 min read
Microsoft Azure - Automating Azure Blobs Lifecycle Management
In this article, we will learn how to automatically manage Azure Blobs lifecycles. Azure Blob Storage offers different access tiers to reduce storage costs. A Blob in the hot tier costs the most and can be accessed very fast, while a Blob in the archive tier is cheaper and is slower. Let's use Lifecycle Management to automatically move Blobs betwee
2 min read
Automating AWS Network Firewall Configurations with Terraform
The most crucial aspect in protecting and safeguarding network infrastructure in the landscape of cloud computing is applying security to a network. AWS Network Firewall offers high-performance, fully managed security services that provide flexible protection for all your Amazon VPCs to define, implement, and manage network traffic rules that help
8 min read
How to Use Docker For Fault Tolerance with Docker Replicas?
Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. A cluster of docker engines is called a swarm. Docker Swarm is a cluster management and orchestration feature embedded in the Docker Engine. Docker ReplicasDocker replicas refer to running multiple instances of a doc
4 min read
How to Use Docker For Cross-Platform Containerization with Docker Buildx?
Docker has completely modified the manner in which software program is evolved and deployed, with the aid of introducing containerization generation. In order to package programs with their dependencies and make certain consistency of conduct across many contexts, packing containers offer a compact and portable solution. Deploying containerized app
7 min read
How to Use Docker Content Trust to Verify Docker Container Images
The world of containerized applications is based on trust. You rely on Docker images to be exactly what they say they are: secure, reliable, robust, and built with the right elements. But just like you can not just blindly trust any random ingredient in your kitchen, similarly the Docker image needs a kind of verification so we don't need to be con
12 min read