DEV Community

Cover image for Install Docker with Ansible on Ubuntu (Official Repo + Docker Compose)
Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on • Edited on

Install Docker with Ansible on Ubuntu (Official Repo + Docker Compose)

Hello, I'm Maneshwar. I'm working on FreeDevTools online currently building **one place for all dev tools, cheat codes, and TLDRs* — a free, open-source hub where developers can quickly find and use tools without any hassle of searching all over the internet.

If you're managing infrastructure with Ansibl
e, installing Docker the right way — using Docker's official apt repository — ensures you're getting the latest stable version.

This post walks you through setting up Docker + Docker Compose on Ubuntu entirely via Ansible.

What This Playbook Does

  • Updates APT cache
  • Installs required dependencies
  • Adds Docker's official GPG key and APT repo
  • Installs Docker CE, CLI, Compose, Buildx, and Containerd
  • Enables and starts the Docker daemon
  • Adds your user (ubuntu) to the docker group so you don’t need sudo for every Docker command

Ansible Playbook: install-docker.yml

---
- name: Install Docker on Ubuntu using official Docker repo
  hosts: all
  become: true

  tasks:
    - name: Update apt cache
      ansible.builtin.apt:
        update_cache: yes

    - name: Ensure dependencies are installed
      ansible.builtin.package:
        name:
          - bc
          - curl
          - expect
          - git
          - ca-certificates
        state: present

    - name: Create Docker GPG key directory
      ansible.builtin.file:
        path: /etc/apt/keyrings
        state: directory
        mode: "0755"

    - name: Download Docker's official GPG key
      ansible.builtin.get_url:
        url: https://download.docker.com/linux/ubuntu/gpg
        dest: /etc/apt/keyrings/docker.asc
        mode: "0644"

    - name: Add Docker repository to Apt sources
      ansible.builtin.apt_repository:
        repo: "deb [arch={{ ansible_architecture }} signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
        state: present
        filename: docker

    - name: Update apt cache after adding Docker repository
      ansible.builtin.apt:
        update_cache: yes

    - name: Install Docker and Docker Compose
      ansible.builtin.package:
        name:
          - docker-ce
          - docker-ce-cli
          - containerd.io
          - docker-buildx-plugin
          - docker-compose-plugin
        state: present

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

    - name: Add ubuntu user to docker group
      ansible.builtin.user:
        name: ubuntu
        groups: docker
        append: yes
Enter fullscreen mode Exit fullscreen mode

After Running the Playbook

Once this finishes:

  • Logout and log back in to apply the group change.
  • Confirm everything works:
docker --version
docker compose version
docker run hello-world
Enter fullscreen mode Exit fullscreen mode

Why Use This Over apt install docker.io?

  • You get the latest Docker packages direct from Docker Inc.
  • It includes Buildx and Docker Compose v2 (plugin style).
  • Cleaner integration with CI/CD, servers, and dev boxes.

How to Run

ansible-playbook -i inventory install-docker.yml
Enter fullscreen mode Exit fullscreen mode

UUse your own inventory/host setup as needed.

FreeDevTools

I’ve been building FreeDevTools.

A collection of UI/UX-focused tools crafted to simplify workflows, save time, and reduce friction in searching tools/materials.

Any feedback or contributors are welcome!

It’s online, open-source, and ready for anyone to use.

👉 Check it out: FreeDevTools
⭐ Star it on GitHub: freedevtools

Let’s make it even better together.

Top comments (0)