How to Install Docker Compose using Ansible Playbook

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 Compose Ansible

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.

What is Docker Compose?

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.

What is Ansible?

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.

Install Docker Compose using Ansible Playbook

Pre-requisites

Instructions

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. 
  • Verify docker for ubuntu gpg key with fingerprint
  • configure docker for ubuntu stable repository
  • Install docker engine for ubuntu
  • Add remote user to docker group
  • Finally, Install docker-compose

Update Existing Ubuntu Packages

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.

				
					- hosts: all
  become: yes
  gather_facts: false
  tasks:
  - name: 
    remote_user: 
    tags:
  - name: 
    remote_user: 
    tags:
  - name:
    remote_user: 
    tags:
      
  
				
			

In the above template, we have a list of tasks, and each task has a name and other parameters, which we will see next.

Add Docker Packages

This will be our first task, and we will add docker packages that are required for docker-compose.

				
					- hosts: all
  become: yes
  gather_facts: false
  tasks:
  - name: Install docker packages
    remote_user: ansible_ubuntu_demo
    apt:
      name: name: ['apt-transport-https','ca-certificates', 'curl', 'software-properties-common']
      state: present
      update_cache: yes
    tags:
      - docker
  
				
			

Add Docker's gpg Key

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.

				
					- name: Configure Docker for ubuntu stable repository
    remote_user: ansible_ubuntu_demo
    apt_repository:
      repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable
      state: present
      update_cache: yes
    tags:
      - docker
				
			

Update Ubuntu packages (optional)

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. 

				
					- name: Update apt packages
    remote_user: ansible_ubuntu_demo
    apt:
      update_cache: yes
    tags:
      - docker
				
			

Install Docker-Ce

Next, the script will be for installing docker. As without installing the docker engine, it is impossible to install docker-compose.

				
					- name: Install docker-ce
    remote_user: ansible_ubuntu_demo
    apt:
      name: docker-ce
      state: present
      update_cache: yes
    tags:
      - docker
				
			

Add Remote User to Docker Group

We will now add remote_user to the docker group.

				
					 - name: Add "ansible_ubuntu_demo" user to "docker" group
    remote_user: ansible_ubuntu_demo
    user:
      name: "ansible_ubuntu_demo"
      group: "docker"
      append: yes
    tags:
      - docker
				
			

Install Docker-Compose

As a final step, we will install docker-compose from the official GitHub repository. With this step, our configuration file is complete.

				
					- name: Install docker-compose from official github repo
    remote_user: ansible_ubuntu_demo
    get_url: 
      url : https://github.com/docker/compose/releases/download/1.29.2/docker-compose-Linux-x86_64
      dest: /usr/local/bin/docker-compose
      mode: 'u+x,g+x'
				
			

Run the Config file

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. 

Avatar for Bhaskar Narayan Das
Bhaskar Narayan Das

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.

5 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x