Hassle-free docker installations with Ansible on Ubuntu 22.04

jimfilippou

Dimitrios Filippou

Posted on November 11, 2023

Hassle-free docker installations with Ansible on Ubuntu 22.04

🚀 Automating Docker CE Installation on Ubuntu with Ansible 🚀

Greetings DEV community! 👋 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 logo

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

<span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Install dependencies via apt</span>
  <span class="na">apt</span><span class="pi">:</span>
    <span class="na">name</span><span class="pi">:</span>
      <span class="pi">-</span> <span class="s">ca-certificates</span>
      <span class="pi">-</span> <span class="s">curl</span>
      <span class="pi">-</span> <span class="s">gnupg</span>

<span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Add Docker's official GPG key</span>
  <span class="na">apt_key</span><span class="pi">:</span>
    <span class="na">url</span><span class="pi">:</span> <span class="s">https://download.docker.com/linux/ubuntu/gpg</span>
    <span class="na">state</span><span class="pi">:</span> <span class="s">present</span>

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

<span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Update apt cache</span>
  <span class="na">apt</span><span class="pi">:</span>
    <span class="na">update_cache</span><span class="pi">:</span> <span class="s">yes</span>

<span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Install Docker</span>
  <span class="na">apt</span><span class="pi">:</span>
    <span class="na">name</span><span class="pi">:</span> 
      <span class="pi">-</span> <span class="s">docker-ce</span>
      <span class="pi">-</span> <span class="s">docker-ce-cli</span>
      <span class="pi">-</span> <span class="s">containerd.io</span>
      <span class="pi">-</span> <span class="s">docker-buildx-plugin</span>
      <span class="pi">-</span> <span class="s">docker-compose-plugin</span>
    <span class="na">state</span><span class="pi">:</span> <span class="s">present</span>

<span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Add user to Docker group</span>
  <span class="na">user</span><span class="pi">:</span>
    <span class="na">name</span><span class="pi">:</span> <span class="s">root</span> <span class="c1"># Please use another user, not root :-)</span>
    <span class="na">append</span><span class="pi">:</span> <span class="s">yes</span>
    <span class="na">groups</span><span class="pi">:</span> <span class="s">docker</span>
Enter fullscreen mode Exit fullscreen mode
Enter fullscreen mode Exit fullscreen mode




Conclusion

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

💖 💪 🙅 🚩
jimfilippou
Dimitrios Filippou

Posted on November 11, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related