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 building git-lrc, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. Star Us to help devs discover the project. Do give it a try and share your feedback for improving the product.

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

Use your own inventory/host setup as needed.

git-lrc
*AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.*

Any feedback or contributors are welcome! It's online, source-available, and ready for anyone to use.

⭐ Star it on GitHub:

GitHub logo HexmosTech / git-lrc

Free, Unlimited AI Code Reviews That Run on Commit




AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

See It In Action

See git-lrc catch serious security issues such as leaked credentials, expensive cloud operations, and sensitive material in log statements

git-lrc-intro-60s.mp4

Why

  • 🤖 AI agents silently break things. Code removed. Logic changed. Edge cases gone. You won't notice until production.
  • 🔍 Catch it before it ships. AI-powered inline comments show you exactly what changed and what looks wrong.
  • 🔁 Build a

Top comments (0)