How to Install Docker using Ansible [Debian/Ubuntu]

How to install Docker using Ansible


This article will showcase a process how to install Docker using Ansible. We’ll be creating an Ansible playbook which will contain only five lines of code. What will enable us to have such a small playbook is the official Docker install script, which will automate the Docker install process for us. The script works on many major Linux distributions such as – Debian, Ubuntu, Fedora, RedHat, CentOS, OpenSuse and Raspbian. The script we’ll be using can be looked at and downloaded at – docker install script. For this example we will install Docker using Ansible on Debian 11 virtual machine.

Their script does not install the Docker compose though, but still it’s an awesome script.

This process can be applied on any type of Linux machine – bare metal, VM or a cloud server.

Another side note – we also covered a process how to deploy Docker containers using Ansible – The process can be looked at this post, and how to deploy Nextcloud and WordPress on Docker using Ansible.


In order to successfully install Docker using Ansible, the following prerequisites are required:

  • Enabled SSH access on the host group(linux server machines/vms)
  • A user with sudo privileges(optional – to use root account)
  • Installed curl tool on Linux servers

What is Ansible?

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows.

Create the host file with inventory and variables

First we need to create a hosts file with the remote nodes and to set login variables:

sudo nano /etc/ansible/hosts

In the text file, add the host parameters to look something like this:



ansible_user=sudouser1 ansible_password=user1password ansible_become_password=user1password

Save the file and exit.

Hosts file breakdown:

[docker] – Remote host(vm/server) IP address under the host group named docker


ansible_user – username of the user that has sudo privileges(also you can set the root user)

ansible_password – sudo user password

ansible_become_password – sudo user password (for sudo privileges when running commands that require sudo access to be ran)

install docker using ansible

Create the .yaml playbook

Next step for us to create the .yaml playbook in which we need to add the configuration. You can create playbook file anywhere you wish:

nano docker-install.yml

Once the text editor opens, copy and paste in the configuration from the bellow(mind the indentation) and save the file.

- hosts: docker
  - name: Install Docker cd ~ && curl -fsSL -o && sudo sh

Playbook breakdown:

hosts: docker – Specified that this book will run and be applied only on our docker host group.

tasks: – Specified that there’s a task to run

name: name of the task that we’re running

ansible.builtin.sheel: – Ansible shell module that allows us to run bash shell commands on our remote Linux servers

The shell command we ran in our Ansible playbook:

“cd ~ && curl -fsSL -o && sudo sh”

The mentioned shell command will do following: It will navigate first to the user’s home directory, download the Docker install script on home directory and run the same install script.

install docker using ansible

Run the playbook

When we have our resources, machines and playbook file ready, we can run the playbook to initialize the Docker install process. Run the following to run the playbook only for the specific host group:

ansible-playbook docker-install.yml -l docker

Once you started the playbook, it will take approximately 10 minutes to finish(maybe a bit more, it depends on numerous factors).

install docker using ansible

When Ansible finishes running the playbook and you get a successful report like from the above picture, we can double check if the Docker is installed successfully. Docker should be running right away and we can check it with the command:

sudo systemctl status docker.service

sudo docker ps

install docker using ansible

install docker using ansible


To summarize the article – we performed the steps how to install Docker using Ansible. Hosts file was created in which we added our remote node with login variables for the SSH connection, wrote and ran a .yaml playbook which downloads the official Docker automated install script and runs the same script which installs the Docker automatically. It’s thanks to this script, our .yaml playbook consists of five lines of config only.

Since the Docker installation is possible with Ansible, it’s also possible to deploy Docker containers as well. If interested, you can check out the process at this post.

Thank you for your time…

You may also like...

2 Responses

  1. PR says:

    “Packages for RHEL are currently only available for s390x”

    • MarkoNtech says:

      That is correct, thank you for mentioning it. Docker also refereed that it is also possible to install docker centos packages on rhel –
      Best regards…

Leave a Reply

Your email address will not be published.