Menu
Faastic Logo
Faastic Logo
Install Docker on Ubuntu with Ansible

Install docker on Ubuntu with Ansible

Posted on November 8, 2023November 8, 2023 by faastic

Here is a quick playbook to install and manage Docker with Ansible on Ubuntu hosts. 

Don’t forget to add the host to the docker-hosts file. 

Here is our ready to use docker playbook:

---
- name: Install Docker and Docker Compose
  hosts: docker-hosts
  become: yes
  tasks:
  
  - name: Install aptitude using apt
    apt: 
      name: aptitude
      state: latest
      update_cache: yes

  - name: Install pip3
    apt:
      name: python3-pip
      update_cache: yes
      state: present

  - name: Install Docker SDK for Python
    ansible.builtin.pip:
      name: docker
      state: present

  - name: Install Docker SDK for Python
    ansible.builtin.pip:
      name: docker-compose
      state: present

  - name: Install required system packages
    apt: 
      name: "{{ packages }}"
      state: latest
      update_cache: yes
    vars:
      packages:
        - apt-transport-https
        - ca-certificates
        - curl
        - gnupg
        - lsb-release
        - software-properties-common

  - name: Add Docker’s official GPG key
    ansible.builtin.apt_key:
      url: https://download.docker.com/linux/ubuntu/gpg
      state: present

  - name: Add Docker repository
    ansible.builtin.apt_repository:
      repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_lsb['codename'] }} stable
      state: present

  - name: Install Docker Engine
    apt: 
      name: docker-ce
      state: latest
      update_cache: yes

  - name: Ensure Docker service is running
    ansible.builtin.service:
      name: docker
      state: started
      enabled: yes

Here’s a step-by-step breakdown of what each part of the playbook does:

  1. Set the target hosts and become privileged: It specifies that the following tasks should be run on the hosts grouped under docker-hosts and that you should escalate to privileged (root) access using the become: yes directive.
  2. Install aptitude: The first task uses the apt module to install aptitude, a package manager for Debian and Ubuntu, ensuring it’s the latest version and updates the package cache.
  3. Install pip3: The next task installs python3-pip, the Python package installer, ensuring it’s present on the system and again updates the package cache.
  4. Install Docker SDK for Python: This involves two tasks that use the ansible.builtin.pip module to install the Docker SDK (docker) and Docker Compose (docker-compose) for Python.
  5. Install required system packages: Several key system packages are installed, such as curl, gnupg, and lsb-release. This task also demonstrates the use of variables within Ansible by using {{ packages }} to represent a list of package names defined under vars.
  6. Add Docker’s official GPG key: This uses the ansible.builtin.apt_key module to add the official Docker GPG key to the list of keys used by the APT package manager to authenticate packages.
  7. Add Docker repository: The ansible.builtin.apt_repository module adds the official Docker repository to the list of APT sources.
  8. Install Docker Engine: The apt module is used again to install docker-ce (Docker Community Edition), ensuring that the latest version is installed and the package cache is updated.
  9. Ensure Docker service is running: Finally, the ansible.builtin.service module ensures that the Docker service is started and enabled to start on boot.

Suggestions and tips? Leave a comment!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to install Ansible
  • Execute Ansible playbooks from github actions
  • Install docker on Ubuntu with Ansible
  • Why use ansible to manage your Infrastructure
  • Why you need an off-site backup strategy

Recent Comments

No comments to show.

Archives

  • November 2023

Categories

  • Ansible
  • Backups
©2025 sauce.faastic.com | Powered by Superb Themes