Install Docker on Ubuntu with Ansible

Install Docker on Ubuntu with Ansible

I like to use Docker as much as possible for Jenkins builds since it simplifies the agent infrastructure – the agents just need to have git, java (jdk or jre) and Docker installed. I typically install Docker on Ubuntu via the instructions on the Docker website. The instructions recommend using the Docker apt repository which ensures you always have access to the latest version of Docker. Naturally, I use Ansible for managing the configuration of the build agents – it's a HUGE time saver.

With that, I thought I'd share some nice Ansible tasks that I used to install Docker from the Docker apt repository:

- name: Install apt packages
  apt:
    update_cache: yes
    name:
      - apt-transport-https
      - ca-certificates
      - curl
      - gnupg-agent
      - software-properties-common
    state: present
    
- name: Add Docker GPG key
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
    state: present

- name: Add Docker apt repository
  apt_repository:
    repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release | lower }} stable
    state: present
  
 - name: Install docker
  apt:
    update_cache: yes
    name: docker-ce
    state: present

Note that the first task ensures that the agent has the dependencies required for adding the Docker GPG key and the apt repository. I also use {{ ansible_distribution_release | lower }} instead of hard-coding the Ubuntu release version (i.e. bionic).

Show Comments