Microservice oriented development has evolved over the years, and Docker has played a prominent role in the evolution of overall development and deployment methods adopted by the teams. In this article, we will cover the the steps to Install Docker Compose using Ansible Playbook. Without much ado, let’s get started.
What is Docker and the Docker ecosystem
Docker is a software platform that enables applications to be built using containers, which are lightweight and require the operating system kernel but otherwise run separately from one another. The process of preparing software to operate on any kind of hardware has been simpler because of the 2013 launch of the open-source Docker project, which made the use of containers in Linux and Unix systems commonplace for software developers.
Docker is not only a container creation tool but also offers a plethora of tools to help manage, redefine the entire containerization process. These tools form part of the Docker ecosystem. Here are the tools which form the part of the Docker ecosystem:
Docker Client – A CLI (Command line interface) tool that allows the user to pass commands to a docker daemon.
Docker Server– It runs the main daemon and listens to the commands, API calls and based on commands, creates images, runs container and performs several key functions.
Docker Machine – It is used to create Docker Host on the user’s local machine, on the cloud and in the data centre.
Docker Images – Contains application code and script used for the creation and running of the container.
Docker Hub – Central repository provided by Docker Inc for sharing and storing container images.
Docker Compose – Tool that helps in creation and running of multi-container applications.
Docker Swarm – It is the in-house container orchestration tool. Other alternatives are Kubernetes, Amazon ECS, etc.
Docker Compose is a tool for orchestrating the execution of numerous containers as a single service. Each container here runs alone but can communicate with one another as necessary. Docker Compose files are extremely simple to write in YAML, an XML-based scripting language that stands for Yet Another Markup Language. Additionally, Docker Compose enables users to activate all services (containers) with a single command. Docker Compose can run on Linux or Windows.
Ansible is a software application that automates cross-platform computer assistance in a simple but effective way. It’s primarily aimed at IT pros, who use it for application deployment, workstation and server updates, cloud provisioning, configuration management, intra-service orchestration, and practically anything else a systems administrator does on a weekly or daily basis. Ansible server is simple to set up because it doesn’t require any agent software and doesn’t require any additional security architecture.
What is Ansible Playbook?
Ansible code is written as playbooks, which are files. YAML is the format in which playbooks are written. Ansible’s playbooks are one of its most important aspects, as they inform it what to do. They’re similar to a to-do list for Ansible, with a list of tasks.
Playbooks are a collection of steps that a user wants to run on a certain computer. Playbooks are executed in order. Playbooks are the foundation for all of Ansible’s use cases. Read the article Ansible in the cloud to learn more about installing ansible in the clioud.
Incase, you are not familiar with the installation of Ansible, you can follow this official document. We will be using Ubuntu platform and following tasks are required to be added in the yaml file.
Install docker packages necessary to install docker engine.
Add Docker’s GPG key for ubuntu from official site.
The very first step is to update the ubuntu package. Use the following command to update the ubuntu package. All though this task can also be automated using Ansible but we can update it before hand.
sudo apt-get update
Create a New Yaml File for Ansible playbook
Next, we will create an ansible playbook file. This file will follow the YAML format. You can give any name to this file, but the extension should be .yml. The following command will create a YAML file with the name docker.yml
$ vi docker.yml
The above command will create an empty file. If you are not familiar with vi commands then you can check this cheat sheet. Now we will follow this template that will be used by Ansible playbook.
Now, we will add Docker’s gpg key from the official site.
- name: Add Docker s GPG key for ubuntu from official site
remote_user: ansible_ubuntu_demo
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
tags:
- docker
GPG Key with Fingerprint verification
We will add the script to verify the gpg key with the fingerprint.
- name: Verify gpg key with the fingerprint
remote_user: ansible_ubuntu_demo
apt_key:
id: 0EBFCD88
state: present
tags:
- docker
Configure Docker Repo
It is time to add official stable docker release repo for ubuntu.
This is an optional task. If you want Ansible to take care of all the stuff such updating to existing ubuntu package then you can add this task. Else, the first step will also update the package.
The config file can be executed using the following command to complete the installation:
$ sudo ansible-playbook docker.yml
Ansible is an automation tool, and it can be used to automate a large number of tools. You can choose to install docker using Ansible playbook or choose to run Ansible from the docker container. In this article, we have learned to install docker-compose using an Ansible playbook. For efficient execution and achieving full automation, Ansible is the best fit. It is a must tool for every enterprise.
Data analytics, Cloud development and software development are my passions. I have extensive knowledge in Java and the AWS platform. I am currently one of Cloud Infrastructure Services technical writers.