> ## Content Index
> Fetch the complete content index at: https://horrell.ca/llms.txt
> Use this file to discover other available public pages before exploring further.

# Install Docker on Ubuntu with Ansible
- URL: https://horrell.ca/2020/06/18/installing-docker-on-ubuntu-with-ansible/
- Published: 2020-06-18T13:00:00.000Z
- Updated: 2020-10-10T23:33:23.000Z
- Author: Christopher Horrell
- Tags: ansible, docker, #Import 2024-10-01 02:36

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](https://docs.docker.com/engine/install/ubuntu/?ref=horrell.ca). The instructions recommend using the Docker apt repository which ensures you always have access to the latest version of Docker. Naturally, I use [Ansible](https://www.ansible.com/?ref=horrell.ca) 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:

```yml
- 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`).