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 .



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads