DEV Community

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

Posted on

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

Hi there! I'm Maneshwar. Right now, I’m building LiveAPI, a first-of-its-kind tool that helps you automatically index API endpoints across all your repositories. LiveAPI makes it easier to discover, understand, and interact with APIs in large infrastructures.


If you're managing infrastructure with Ansible, 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

Use your own inventory/host setup as needed.


LiveAPI helps you get all your backend APIs documented in a few minutes.

With LiveAPI, you can generate interactive API docs that allow users to search and execute endpoints directly from the browser.

LiveAPI Demo

If you're tired of updating Swagger manually or syncing Postman collections, give it a shot.

Top comments (0)