Open In App

How To Install Docker Using Ansible Playbook ?

Last Updated : 02 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 make, convey, and oversee programming applications has been totally changed by Docker. An incredible asset packs applications into lightweight, convenient compartments, making it simpler to construct, boat, and use them.

What is Docker?

Docker infrastructure consists of – client (docker build, dicker pull, docker run), docker host (docker daemon, containers, images), Registry (images as snapshots)

Docker Infrastructure

Docker consists of a client server, Docker host, and Registry. Initially, with the help of client-server we need to install docker and start, and enable the docker services. Then, we can to build the images with the help of a dockerfile by using the docker build command.

Now, from the client server the pull request is sent to the Docker host, where the docker Daemon is the engine of the docker receives the request and the docker engine sent this request to registry {i.e; Docker-Hub, where the images are stored}. From the registry, it takes the required image and sent back to the docker host and pulls the images, and runs the container with the help of the images. inside these containers, the applications runs.

Key-Concepts of Docker

  • Containerization: Applications and their dependencies are packaged into containers by Docker, which makes use of containerization technology. Containers are isolated environments with their own filesystem, processes, and networking despite sharing the kernel of the host operating system.
  • Images: Docker pictures are perused just formats that contain everything expected to run a compartment, including the application code, runtime, libraries, and conditions. Pictures are utilized as the reason for making holders.
  • Containers: Containers are runtime environments that run instances of Docker images and are portable, lightweight, and isolated. Regardless of the underlying infrastructure, they provide a consistent environment for applications to run in.
  • Docker Daemon: Docker Daemon is the center part of Docker that empowers the creation and the executives of holders. It consists of a command-line interface (CLI) tool called Docker, which enables users to interact with the Docker daemon, and a daemon process called Dockerd, which runs on the host machine.

Primary Terminologies

Docker

Docker is a platform that empowers developers to develop, test, and run applications in containers. containers are lightweight, convenient, and isolated conditions that package applications and their conditions. Docker gives tools and APIs to building, managing, and deploying containers productively.

Ansible

Ansible is an open-source automation tool utilized for setup the management, application deployment, and task automation. It utilizes YAML-based playbooks to depict the ideal condition of frameworks and executes tasks on remote hosts over SSH. Ansible improves on infrastructure management via automating repetitive tasks and ensuring consistency across environment.

Inventory

A stock in Ansible is a document that characterizes the rundown of hosts and groups of hosts that Ansible will make due. It can be written in INI or YAML, and it can be static or dynamic. The stock document indicates the association data, (for example, IP addresses or hostnames) for each host and puts together has into groups for more easier administration.

Playbook

A playbook in Ansible is a YAML file containing a set of instruction (tasks) that define the ideal condition of remote hosts. Playbooks are utilized to automate tasks like programming installation, configuration, and support. There are one or more plays in each playbook that specify the hosts to target and the actions to take.

Module

Ansible’s modules are small programs that carry out particular operations on remote hosts. Ansible gives many underlying modules for normal tasks like package management, file manipulation, and administration control. Modules are executed by the Ansible engine on the target host and convey the outcomes back to the control node.

Task

An task in an Ansible playbook is a single unit of work that characterizes an activity to be performed on a remote host. Tasks can carry out actions like installing packages, copying files, or restarting services in a sequential manner. The module to be used and the parameters needed to carry out the action are specified in each task.

Step-by-step process to install docker using ansible playbook:

Launch two EC2 Instances, one is master and another one is slave. with the help of master-slave configuration, we need to install docker package into slave instance through, master instance.

Step 1: Launch an instance

list of instances

  • now, make master-slave configuration.
  • Connect the master instance and check for installed packages
$cd /etc/ → $ ls (check for ansible..) if it is not installed

Step 2: Install Ansible

$sudo amazon-linux-extras install ansible2
  • Now go to /etc/ and check for ansible…. If package Installed, then go to hosts file and make a
  • Master Slave configuration
$cd /etc/ → ls → cd ansible → sudo vi hosts

Master-slave configuration

  • Save and exit
  • Now, check for newly added ip address from master instance terminal by using command; $ ansible all –list-hosts

slave instance connected

  • Now, make a communication to all the slave instances from Master instance by using command; $ ansible all -m ping

successful master-slave configuration

  • Master – Slave Configuration done successful…!!
  • Now, with the help of ansible playbook, we need to install docker package into the instance.
  • So, Now by using Master server ..we are going to install docker package in Slave instance…for
  • that am going to create a playbook

Step 3: Create Playbook

  • Make a directory for playbooks need to perform to communicate with all the rest Slave servers…

$ mkdir playbooks

ls

  • Go to that directory and create a .yml extension filed playbook to install Docker package.
$sudo vi package.yml

Ansible playbook

  • save the file.

Step 4: Run the Playbook

  • Execute the playbook, by using following command
$ansible-playbook package.yml

Docker installation successful using ansible-playbook

Step 5: Verify

  • Now, check Docker is installed or not in slave servers. In slave instance terminal, using command, $ docker -version

Docker installed in slave server

  • Docker enables you to build, test and deploy applications and package applications and their dependencies into containers, ensuring consistency across different environments. Installing Docker on Amazon Linux AMI enables users to leverage Docker’s capabilities for their applications running on AWS infrastructure.

Conclusion

Taking everything into account, introducing Docker on track has with Ansible has number of benefits, including versatility, consistency, and productivity. Structure heads and DevOps gatherings can streamline the association of Docker across different circumstastand-aloneAnsible playbook – FAQ’s

Why utilize a playbook while introducing Docker on Amazon Linux, and what is a playbook?

A playbook is like a recipe/script that tells Ansible how to set up your servers/instances. We use playbooks for Docker foundation to computerize the cooperation, committing it speedier and less error slanted.

Could I at any point involve playbooks for Docker establishment without knowing how to code?

Actually, no! The language used in playbooks (YAML) is straightforward and direct, easy to understand. You basically need to follow the means outlined in the playbook.

Can I modify the Docker establishment procedure with a playbook at any time?

Absolutely! Playbooks are versatile, so you can transform them to suit your necessities, according to prerequisites. Do you want to set up more advanced settings or install specific versions of Docker? Playbooks work on things.

Might I anytime present Docker on Amazon Linux without a playbook?

Despite the fact that it is feasible to physically introduce Docker, utilizing a playbook will save you time and exertion, especially assuming you have a ton of servers.

What occurs on the off chance that something turns out badly during a playbook-based Docker establishment?

Ansible is smart enough to handle mistakes and retries naturally. If something goes wrong, it will let you know so you can fix it.

On Amazon Linux, how would I execute a playbook to introduce Docker?

It’s basically pretty much as straightforward as running a single request! Basically tell Ansible where your servers are and which playbook to use, and it wraps up.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads