Skip to main content
🎤 Luca Berton is speaking at Red Hat Summit & KubeCon EU 2026!Learn more →
Back to Blog

Ansible Playbook for Docker Install

Automate Docker installation on Ubuntu and RHEL with an Ansible playbook. Includes Compose plugin and post-install steps.

Luca BertonApril 10, 20261 min read

The Playbook

This playbook installs Docker CE on Ubuntu and RHEL/CentOS, adds the Docker Compose plugin, and configures a non-root user:

yaml
---
- name: Install Docker
  hosts: all
  become: true
  vars:
    docker_user: "{{ ansible_user }}"

  tasks:
    - name: Install prerequisites (Ubuntu)
      ansible.builtin.apt:
        name:
          - ca-certificates
          - curl
          - gnupg
        state: present
        update_cache: true
      when: ansible_os_family == "Debian"

    - name: Add Docker GPG key (Ubuntu)
      ansible.builtin.apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present
      when: ansible_os_family == "Debian"

    - name: Add Docker repository (Ubuntu)
      ansible.builtin.apt_repository:
        repo: "deb https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
        state: present
      when: ansible_os_family == "Debian"

    - name: Install Docker (Ubuntu)
      ansible.builtin.apt:
        name:
          - docker-ce
          - docker-ce-cli
          - containerd.io
          - docker-compose-plugin
        state: present
        update_cache: true
      when: ansible_os_family == "Debian"

    - name: Install prerequisites (RHEL)
      ansible.builtin.dnf:
        name:
          - dnf-plugins-core
        state: present
      when: ansible_os_family == "RedHat"

    - name: Add Docker repository (RHEL)
      ansible.builtin.command:
        cmd: dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
      when: ansible_os_family == "RedHat"
      changed_when: true

    - name: Install Docker (RHEL)
      ansible.builtin.dnf:
        name:
          - docker-ce
          - docker-ce-cli
          - containerd.io
          - docker-compose-plugin
        state: present
      when: ansible_os_family == "RedHat"

    - name: Start and enable Docker
      ansible.builtin.systemd:
        name: docker
        state: started
        enabled: true

    - name: Add user to docker group
      ansible.builtin.user:
        name: "{{ docker_user }}"
        groups: docker
        append: true

    - name: Verify Docker installation
      ansible.builtin.command:
        cmd: docker --version
      register: docker_version
      changed_when: false

    - name: Show Docker version
      ansible.builtin.debug:
        msg: "{{ docker_version.stdout }}"

Run It

bash
# Single server
ansible-playbook docker-install.yml -i "server1," -u ubuntu

# Multiple servers from inventory
ansible-playbook docker-install.yml -i inventory.ini
Related Course

Master this topic with hands-on labs

Go beyond reading — build real projects in sandboxed environments with expert video guidance.

Browse Courses →

Inventory Example

ini
[docker_hosts]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11
db1  ansible_host=192.168.1.20

[docker_hosts:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=~/.ssh/id_rsa

Add Docker Compose Standalone (Optional)

If you need the standalone docker-compose binary:

yaml
    - name: Install Docker Compose standalone
      ansible.builtin.get_url:
        url: "https://github.com/docker/compose/releases/latest/download/docker-compose-linux-{{ ansible_architecture }}"
        dest: /usr/local/bin/docker-compose
        mode: '0755'
Stay Updated

Get weekly IT automation tips

Docker, Ansible, Terraform, MLOps — curated insights delivered to your inbox. No spam.

Subscribe Free →

Configure Docker Daemon

Add custom daemon settings:

yaml
    - name: Configure Docker daemon
      ansible.builtin.copy:
        content: |
          {
            "log-driver": "json-file",
            "log-opts": {
              "max-size": "10m",
              "max-file": "3"
            },
            "default-address-pools": [
              {"base": "172.17.0.0/16", "size": 24}
            ]
          }
        dest: /etc/docker/daemon.json
        mode: '0644'
      notify: Restart Docker

  handlers:
    - name: Restart Docker
      ansible.builtin.systemd:
        name: docker
        state: restarted

Idempotent and Safe

Run this playbook multiple times — Ansible only changes what is needed. If Docker is already installed and running, nothing happens.

---

Ready to go deeper? Check out our hands-on course: Ansible Quickstart — practical exercises you can follow along on your own machine.

Ready to learn by doing?

Stop reading tutorials — start building. Expert video courses with hands-on labs in real sandboxed environments.

Share this article
LB
Luca Berton

Docker Captain, IT automation expert, Red Hat Summit & KubeCon speaker. Building hands-on education for DevOps engineers at CopyPasteLearn.

Related Articles

Explore topics

Browse more articles on the topics covered here.