AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible Install Docker: Automate Docker Installation on Linux (Complete Guide)

By Luca Berton · Published 2024-01-01 · Category: installation

How to install Docker on Linux servers using Ansible. Automate Docker Engine installation on Ubuntu, Debian, RHEL, CentOS with a complete playbook.

Ansible Install Docker: Automate Docker Installation on Linux (Complete Guide)

Automate Docker Engine installation across your entire fleet using Ansible. This guide provides complete, tested playbooks for Ubuntu, Debian, RHEL, CentOS, and Fedora — from adding the repository to configuring the Docker daemon.

See also: Ansible on Alpine Linux 3.20: Container Engine Setup Complete Guide

Quick Playbook (Ubuntu/Debian)

---
- name: Install Docker on Ubuntu/Debian
  hosts: all
  become: true
  tasks:
    - name: Install prerequisites
      ansible.builtin.apt:
        name:
          - ca-certificates
          - curl
          - gnupg
          - lsb-release
        state: present
        update_cache: true

- name: Add Docker GPG key ansible.builtin.apt_key: url: https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg state: present

- name: Add Docker repository ansible.builtin.apt_repository: repo: "deb https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable" state: present filename: docker

- name: Install Docker Engine ansible.builtin.apt: name: - docker-ce - docker-ce-cli - containerd.io - docker-buildx-plugin - docker-compose-plugin state: present update_cache: true

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

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

RHEL / CentOS / Fedora Playbook

---
- name: Install Docker on RHEL/CentOS/Fedora
  hosts: all
  become: true
  tasks:
    - name: Remove old Docker packages
      ansible.builtin.dnf:
        name:
          - docker
          - docker-client
          - docker-common
          - docker-engine
        state: absent

- name: Install prerequisites ansible.builtin.dnf: name: - dnf-plugins-core state: present

- name: Add Docker repository ansible.builtin.yum_repository: name: docker-ce description: Docker CE Stable baseurl: "https://download.docker.com/linux/{{ 'rhel' if ansible_distribution == 'RedHat' else ansible_distribution | lower }}/{{ ansible_distribution_major_version }}/x86_64/stable" gpgcheck: true gpgkey: "https://download.docker.com/linux/{{ 'rhel' if ansible_distribution == 'RedHat' else ansible_distribution | lower }}/gpg" enabled: true

- name: Install Docker Engine ansible.builtin.dnf: name: - docker-ce - docker-ce-cli - containerd.io - docker-buildx-plugin - docker-compose-plugin state: present

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

See also: Ansible on Amazon Linux 2023: Container Engine Setup Complete Guide

Multi-OS Playbook (Handles Any Distro)

---
- name: Install Docker on any Linux
  hosts: all
  become: true
  
  tasks:
    - name: Include OS-specific tasks
      ansible.builtin.include_tasks: "tasks/docker-{{ ansible_os_family | lower }}.yml"

Configure Docker Daemon

    - name: Create Docker daemon configuration
      ansible.builtin.copy:
        content: |
          {
            "log-driver": "json-file",
            "log-opts": {
              "max-size": "10m",
              "max-file": "3"
            },
            "default-address-pools": [
              {"base": "172.17.0.0/12", "size": 24}
            ],
            "storage-driver": "overlay2",
            "live-restore": true
          }
        dest: /etc/docker/daemon.json
        mode: '0644'
      notify: restart docker

handlers: - name: restart docker ansible.builtin.systemd: name: docker state: restarted

See also: Ansible on Arch Linux: Container Engine Setup Complete Guide

Add Users to Docker Group

    - name: Add multiple users to docker group
      ansible.builtin.user:
        name: "{{ item }}"
        groups: docker
        append: true
      loop:
        - deploy
        - jenkins
        - "{{ ansible_user }}"

Install Docker Compose (Standalone)

Docker Compose v2 is included as a Docker plugin (docker compose). For the standalone binary:

    - name: Install Docker Compose standalone
      ansible.builtin.get_url:
        url: "https://github.com/docker/compose/releases/download/v{{ compose_version }}/docker-compose-linux-x86_64"
        dest: /usr/local/bin/docker-compose
        mode: '0755'
      vars:
        compose_version: "2.29.0"

Verify Installation

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

- name: Show Docker version ansible.builtin.debug: var: docker_version.stdout_lines

- name: Run test container community.docker.docker_container: name: hello-test image: hello-world state: started auto_remove: true register: hello_result

- name: Verify test passed ansible.builtin.debug: msg: "Docker is working correctly" when: hello_result is succeeded

Install Specific Docker Version

    - name: Install specific Docker version (Ubuntu)
      ansible.builtin.apt:
        name:
          - "docker-ce=5:{{ docker_version }}*"
          - "docker-ce-cli=5:{{ docker_version }}*"
          - containerd.io
        state: present
      vars:
        docker_version: "27.0.0"

- name: Pin Docker version (prevent auto-upgrade) ansible.builtin.dpkg_selections: name: "{{ item }}" selection: hold loop: - docker-ce - docker-ce-cli

Complete Role Structure

For production use, organize as an Ansible role:

roles/docker/
├── defaults/main.yml
├── handlers/main.yml
├── tasks/
│   ├── main.yml
│   ├── debian.yml
│   └── redhat.yml
├── templates/
│   └── daemon.json.j2
└── vars/
    ├── Debian.yml
    └── RedHat.yml

Or use the popular community role:

ansible-galaxy role install geerlingguy.docker
- hosts: all
  roles:
    - role: geerlingguy.docker
      docker_users:
        - deploy

FAQ

How do I install Docker with Ansible?

Add the official Docker repository, install docker-ce, docker-ce-cli, and containerd.io packages, then start and enable the Docker service with ansible.builtin.systemd. See the complete playbooks above for Ubuntu/Debian and RHEL/CentOS.

Can Ansible install Docker on multiple servers at once?

Yes. Define your target servers in the Ansible inventory, then run the Docker installation playbook against all of them simultaneously. Ansible handles parallel execution across your entire fleet.

How do I add a user to the Docker group with Ansible?

Use the ansible.builtin.user module with groups: docker and append: true. This adds the user to the docker group without removing them from existing groups, allowing them to run Docker commands without sudo.

Should I use a role or write my own playbook for Docker?

For quick setups, the playbooks above work well. For production, use the geerlingguy.docker Galaxy role or create your own role. Roles provide better organization, reusability, and variable management.

How do I install Docker Compose with Ansible?

Docker Compose v2 is included as a Docker plugin when you install docker-compose-plugin. Run docker compose version to verify. For the standalone binary, download it with get_url from the GitHub releases page.

Conclusion

Automating Docker installation with Ansible ensures consistent, repeatable deployments across your infrastructure. Use the OS-specific playbooks for quick setup, configure the Docker daemon for production logging and networking, and add users to the docker group for secure, sudo-free Docker access.

Related Articles

Ansible Docker Containers: community.docker GuideAnsible on AWS: Automate EC2, S3, IAMAnsible Roles: Create Reusable Automation

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home