Installing Docker and Docker Compose on Ubuntu 20.04 with Ansible
By Tony Mackay ·
This tutorial will show you how to install Docker and Docker Compose on multiple Linux servers by running one simple command.
Prerequisites
Before we begin you’ll need access to a clean installation of Ubuntu 20.04. This tutorial shows you how to install it on VMware Fusion (or VMware Workstation if you’re a Windows or Linux user).
You should also install Ansible on your machine. The steps in this tutorial have been tested on a MacBook Pro with Ansible 2.9.13.
Step 1: Create Inventory
Create the folders to store the Ansible playbook and inventory files.
Let’s call it ansible-docker.
mkdir ~/ansible-docker
mkdir ~/ansible-docker/inventory
touch ~/ansible-docker/inventory/hosts.yml
touch ~/ansible-docker/docker.yml
Open the hosts.yml file and add one or more hosts like shown in the example below.
all:
hosts:
gtsrv01:
gtsrv02:
gtsrv03:
The IP addresses of the hosts need to be resolvable by DNS or by adding them to your local hosts file. In the example above, I have specified three hosts that Docker will be installed on and made the IP addresses resolvable by adding them to my Macs /etc/hosts
file.
Here’s what it looks like:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
10.1.1.21 gtsrv01
10.1.1.22 gtsrv02
10.1.1.23 gtsrv03
Step 2: Create Docker Playbook
The next thing we need to do is create the Ansible playbook that will be used to install Docker and Docker Compose on the Ubuntu servers. Edit the playbook called docker.yml and add the following contents:
---
- hosts: all
become: yes
vars:
docker_compose_version: "1.27.4"
tasks:
- name: Update apt cache
apt: update_cache=yes cache_valid_time=3600
- name: Upgrade all apt packages
apt: upgrade=dist
- name: Install dependencies
apt:
name: "{{ packages }}"
state: present
update_cache: yes
vars:
packages:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
- gnupg-agent
- name: Add an apt signing key for Docker
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add apt repository for stable version
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable
state: present
- name: Install Docker
apt:
name: "{{ packages }}"
state: present
update_cache: yes
vars:
packages:
- docker-ce
- docker-ce-cli
- containerd.io
- name: Add user to docker group
user:
name: "{{ansible_user}}"
group: docker
- name: Download docker-compose {{ docker_compose_version }}
get_url:
url : https://github.com/docker/compose/releases/download/{{ docker_compose_version }}/docker-compose-Linux-x86_64
dest: ~/docker-compose
mode: '+x'
- name: Check docker-compose exists
stat: path=~/docker-compose
register: docker_compose
- name: Move docker-compose to /usr/local/bin/docker-compose
command: mv ~/docker-compose /usr/local/bin/docker-compose
when: docker_compose.stat.exists
There is one variable that may need changed called docker_compose_version
. It contains the version number of Docker Compose you want to install. As of writing the latest version is 1.27.4.
Step 3: Run Playbook
Run the docker.yml playbook with the following command:
ansible-playbook -i inventory docker.yml -u ubuntu -K
Note: change the user with the one you use to login to your Ubuntu servers.
You should see Ansible run through each of the steps in the playbook on each of the servers defined in the hosts.yml inventory file.

Now you should be able to SSH onto each host and see that docker has been installed. This can be checked with the following commands:
docker --version
docker-compose --version

Conclusion
This tutorial showed you how to install Docker and Docker Compose on multiple machines by issuing one simple command.
We did this by writing an Ansible playbook that can be committed to a source code repository and shared with the team.
This is faster than manually connecting to each host and running shell scripts. Another benefit is configuration changes are documented when changes are made to the playbook via Git commits.
Read Next
- The Best Books to Learn Virtualization, Linux and Automation
- Recommended Tools and Software for System Administrators
- Using Docker Compose to Launch a PostgreSQL Database
- Using Traefik to Route HTTP Requests to Multiple Docker Microservices
Tony is the founder and editor of GraspingTech, a blog that helps developers and business owners deploy modern web applications to the cloud. He has written over one hundred tutorials which have been read by more than a million people. Some of the topics he covers are Linux, Virtualization, DevOps and web development.