Automating Docker Deployments With Ansible
Last Updated :
21 Mar, 2024
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

Step 4 : Now copy the private key of worker node into the master node .
scp -i "Ansible-master.pem" Ansible-worker.pem [email protected]:

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

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

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>

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

Step 9 : Now run the ansible playbook .
ansible-playbook -i Inventoryfile nginx-docker-run.yml

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

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 .
Similar Reads
Achieving Zero Downtime Deployments with Docker
Continuous availability of applications is key in a fast-paced software development environment. The main goal is to maintain continuous services around the clock since users demand it; in addition, a few minutes of downtime can lead to decreased revenues, loss of user trust, and negatively reflect
6 min read
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
15+ min read
How to Use Loops in Ansible for Efficient Automation
In the realm of DevOps and IT automation, Ansible is a very strong tool utilized for automating the provision, configuration, and management of systems. One key feature of Ansible is the execution of tasks in an effective and repeatable manner through the use of loops, with the implementation of loo
6 min read
Deployment Automation with Cloud Build
Software delivery is revolutionized by deployment automation, which helps speed up release cycles, lower errors, and streamline deployment procedures. It gives businesses the ability to deliver continuously, which guarantees a quicker time to market and more agility. Automation makes it possible to
5 min read
Implementing Blue-Green Deployments With Docker
Docker is one of the popular tools for building and running programs in containers. It includes various tools and started with a commercial version for managing containers while supporting an open-source version. Table of Content What Is Blue-Green Deployment?How Does A Blue-Green Deployment Work?St
5 min read
Continuous Deployment with Docker Swarm: Automating Container Releases
In software development, specifically for software update and feature addition, it is a very agile world. Continuous Deployment (CD) is crucial and is supported by such practices to automate the frequent delivery of new features and/or updates in coding changes to the production environment with min
6 min read
How to Deploy a Go Web Application with Docker?
Pre-requisite: Docker A popular containerization tool called Docker enables developers to quickly package programs and their environments, enabling shorter iteration times and greater resource efficiency while delivering the same intended environment each time. A container orchestration tool called
3 min read
Creating Templates with Ansible Template Module
Ansible is indeed powerful IT automation software; in its excellent functionalities, the template module stands out with the functionality to apply dynamic generation of configuration files. This is an essential functionality as one of the challenges in managing environments is the fact that differe
7 min read
Docker at Scale: Handling Large Container Deployments
Docker technology allows you to combine and store your code and its dependencies in a small package called an image. This image can then be used to launch a container instance of your application. Table of Content What is a Docker Container?Why Use Docker Containers?Step-By-Step Guide to Docker at S
5 min read
Docker Swift Application
Swift is a cutting-edge, potent programming language created by Apple. For developers, it is effective and pleasant since it delivers safety, speed, and expressiveness. Common programming errors are reduced by Swift's capabilities, which include type inference, optional types, and automatic memory m
6 min read