In this tutorial ( how-to ) I will show you an easy way to install Docker and Docker Compose on a Ubuntu server 20.04 LTS using the well-known DevOps tool Ansible. How this is a basic setup.

1 Requirements

To start you would need Ansible, now Ansible you can install on any Linux distributor and also on a MacOS.

2 Create an environment where you will be working on.

The name of you environment can be any, but I will use ansible-docker

mkdir /etc/ansible/AnsibleDocker

By default Ansible has its own hosts file, so to not make this tutorial a difficult one let’s use the default hosts file

nano /etc/ansible/hosts

and at the end of the file, let’s add the host where we will be installing the Docker and Docker-compose. 127.0.0.1 replace with your target machine IP. If for some reason your target machine SSH port is different from 22 then you need to enter 127.0.0.1: EX. 127.0.0.1:1111

[AnsibleDocker]
127.0.0.1

3 Docker Playbook

I will show you 2 ways, how you can get the playbook, one is by creating it and the second is by downloading it from GitHub, both ways are good ways, but I prefer to download it.

3.1 Creating the playbook

nano /etc/ansible/AnsibleDocker/playbook.yml

and copy/paste

---
- hosts: all
  become: yes
  vars:
    docker_compose_version: "1.29.2"
  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: 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

When creating this tutorial ( how-to ) the docker-compose version is 1.29.2, but when you are creating or downloading the playbook check the latest version here and change it in line 5

3.2 Downloading the playbook from GitHub

cd /etc/ansible/ && git clone https://github.com/jvalters/AnsibleDocker.git

4. Execute

Once we have created or Downloaded playbook, we can now execute it

ansible-playbook /etc/ansible/AnsibleDocker/playbook.yml --ask-pass

The –ask-pass variable I often use since many servers don’t have SSH keys added yet, so it is much faster and if you need to do an installation only once on the server then there is no point in adding the SSH keys.

After the installation is done login to the target machine and check the versions of Docker and Docker-compose by executing the following commands

docker --version
docker-compose --version

If you have followed this tutorial ( how-to ) then all should be good.