How 
to 
install 
docker 
using 
Ansible 
on 
Ubuntu 
22.04

Automating Docker CE Installation on Ubuntu with Ansible 🚀

Greetings! 👋 I wanted to share a quick guide on automating the installation of Docker CE across various Ubuntu Linux machines, regardless of their hosting environment (AWS, Azure, etc.).

The versatility of this automated process not only simplifies the installation but also enhances scalability, making it an invaluable resource for system administrators and developers in diverse cloud environments.

Why Ansible?

Ansible is the secret sauce behind this seamless automation. It's a powerful open-source tool designed to simplify complex IT tasks, configuration management, and application deployment. With its agentless architecture and human-readable scripts (written in YAML) called playbooks, Ansible empowers both developers and system administrators.

By automating routine tasks, Ansible boosts operational efficiency, reduces errors, and ensures consistency across systems. Whether it's configuring servers, deploying applications, or managing network devices, Ansible is a game-changer in modern IT environments.

Simplify with Ansible

While you could manually follow Docker's documentation here for installation, things get tricky when managing multiple installations across various inventories. Here's where Ansible shines, simplifying the process and ensuring a smooth setup.

Bonus: Ansible Semaphore

For those who appreciate a graphical user interface (GUI) to streamline processes and organize playbooks, consider checking out "Ansible Semaphore" at ansible-semaphore.com. This tool isn't mandatory, but it significantly improves the overall experience. Watch this video for more insights.

TL;DR

Ansible's yaml approach is so readable that I'm just going to dump everything here without breaking it down, so here's a complete version of the Ansible playbook:

- name: Install Docker (including compose)
  hosts: all
  become: true

  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes

    - name: Install dependencies via apt
      apt:
        name:
          - ca-certificates
          - curl
          - gnupg

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

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

    - name: Update apt cache
      apt:
        update_cache: yes

    - name: Install Docker
      apt:
        name:
          - docker-ce
          - docker-ce-cli
          - containerd.io
          - docker-buildx-plugin
          - docker-compose-plugin
        state: present

    - name: Add user to Docker group
      user:
        name: root # Please use another user, not root :-)
        append: yes
        groups: docker

Conclusion

Thanks for reading! Feel free to reach out to me for any questions or just have a chat. 🚀 #DevOps #Automation #Docker #Ansible

Affiliate Links

Fastmail

Game-changer for managing multiple email accounts in one place. New users get 10% off!

Code: 7aa3c189

Tangem wallet

Secure hardware wallet for crypto assets. Get 10% discount on your first purchase!

Code: ZQ6MMC

1Password

The best password manager I've used. Secure, easy to use, and saves countless hours.

Freedom24

Invest safely and get a free stock up to $700 when you open an account.

Code: 2915527

ClickUp

The best way to manage your tasks and projects. Get 10% off your first month!

Hetzner

Solid cloud infra, great support, and a great price. Receive €20 in cloud credits.

Code: 3UskohfB0X36

Using these referral links helps support my work. I only recommend products I use and trust. Thank you for your support!