Dec 02, 2025
Dovydas
7min Read
Installing Docker manually works fine – until you have to do it more than once. That’s precisely the kind of problem Ansible was made to solve. With Ansible, you can automate Docker setup and keep the configuration consistent across every server you manage.
Docker is a platform for running applications in fast, lightweight, and portable environments called containers that run independently from the host system. Ansible is a tool designed to automate the setup of those environments so you don’t have to do it manually every time.
Ansible uses a simple text file called a playbook where you define your server configuration. This can include everything from installing packages to setting up various services. There’s no coding required – just write your playbook once, and Ansible will apply it precisely the same way every time you run it.
This guide will show you how to use Ansible to install Docker on an Ubuntu server. You’ll learn to configure Ansible, create an inventory file, write a playbook, and run it to install and configure Docker automatically. Along the way, you’ll also learn how to verify your setup and customize it for more advanced use cases.
Before you get started, make sure you have:
That should cover everything you’ll need. The rest will be handled step by step in the guide.

This process is divided into six steps, from setting up Ansible to customizing the playbook. Let’s start with installing and configuring Ansible.
Before you can install Docker with Ansible, you’ll need to set up Ansible itself. For this tutorial, you’ll install it on the same server where you plan to install Docker – this machine will act as both the control node and the one being managed. Having everything happen on a single server keeps things simple if you’re just getting started or working with a VPS.
To begin, connect to your Ubuntu server via SSH. Once connected, you’ll need to update the system’s package list. This step makes sure your server has the latest available software and security patches:
sudo apt update
Running this command doesn’t install anything. It simply refreshes the list of packages your system can access – a quick step that helps avoid issues during the Ansible installation.
Now you’re ready to install Ansible. Ubuntu includes it in its standard software libraries, so installation is straightforward:
sudo apt install ansible -y
This command installs the main Ansible tool and everything it needs to run.
Once that’s done, it’s a good idea to check that Ansible was installed correctly:
ansible --version
You should see version details on the screen. If you do, you’re ready to move forward.

That’s it. With Ansible in place, you’re ready to configure which servers it will manage in the next step.
Ansible uses an inventory file to know which servers it should manage. Think of it as a list of hosts that Ansible can connect to and run tasks on. Even if you’re only going to be managing one server, like in this tutorial, you’ll still need to define it in the inventory.
By default, the inventory file is located at /etc/ansible/hosts. You can edit this file directly or create your own. For this guide, we’ll create a simple custom inventory file in your working directory to keep things organized.
Create a file called hosts.ini:
nano hosts.ini
Then, add the following:
[local] localhost ansible_connection=local
Here’s what this means:

If you want to manage multiple remote servers, your inventory must contain their IP addresses or hostnames. You would also need to specify the SSH connection type and the username of the remote host that Ansible would use for connection.
Important! To enhance security, we strongly recommend using SSH keys for Ansible SSH authentication between your control node and remote host.
But for this guide, localhost is all you need. Be sure to replace “server.hostname.tld” with your own server address and “username” with your own username.
[webservers] 192.168.1.10 ansible_connection=ssh ansible_user=username server.hostname.tld ansible_connection=ssh ansible_user=username
Save the file, then you’re done setting up the inventory. Next, you’ll write the playbook – the set of instructions that tells Ansible how to install Docker.
Next, it’s time to write the Ansible playbook, which contains a list of tasks Ansible will execute to install and configure Docker for you. Instead of manually entering commands one by one, you’ll define everything in a structured format, and Ansible will handle the rest.
Create a file called install-docker.yml:
nano install-docker.yml
Then paste in the following content:
- name: Install Docker on Ubuntu
hosts: local
become: true
tasks:
- name: Install required packages
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
state: present
update_cache: true
- name: Add Docker’s official GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add Docker repository
apt_repository:
repo: deb https://download.docker.com/linux/ubuntu focal stable
state: present
- name: Install Docker
apt:
name: docker-ce
state: latest
update_cache: true
Let’s break it down:

Here’s what each of the tasks does:
Writing the playbook this way ensures that Docker is installed in a consistent, repeatable manner, which is exactly what Ansible is designed for. You can reuse this file if you need to do it again on another Ubuntu machine.
Save and close the file – you’re now ready to run the playbook.
To execute the playbook, use the following command:
ansible-playbook -i hosts.ini install-docker.yml
Here’s what’s happening in this command:
After you run the command, Ansible begins connecting to the server listed in your inventory. Since you’re using localhost with ansible_connection=local, it doesn’t use SSH. Instead, it runs the tasks directly on the same machine upon which Ansible is installed.
You’ll see each task printed out in the terminal as it runs along with a success or failure message. This makes it easy to spot any issues and monitor progress.

Behind the scenes, Ansible checks if each task needs to be run or if it’s already been completed. For example, if Docker is already installed on the target host, Ansible will skip that step. This is one of the key benefits of using Ansible: it only runs steps when they are needed.
Once the playbook finishes, Docker should be installed and ready to use. The next step is to make sure everything worked.
Once the playbook finishes running, check if Docker was installed correctly. First, check the version to confirm that Docker is available:
docker --version
If the installation worked, you’ll see the version number printed in the terminal.

Next, make sure the Docker service is running:
sudo systemctl status docker
You should see a status of active (running). Press q to exit the status screen.

Finally, run a quick test that pulls a small Docker image from Docker Hub, the default image registry, and runs it in a container:
sudo docker run hello-world
If everything is set up successfully, you’ll see a Hello from Docker! message printed out.

With Docker installed and verified, you’re ready to customize your Ansible playbook.
Now that you’ve successfully installed Docker, you can expand your playbook to include additional tools and configurations.
One popular addition is Docker Compose. It allows you to define and run multi-container applications. To add it to your playbook, you could include a task that downloads the latest version and places it in your system’s binary directory.
- name: Install Docker Compose get_url: url: https://github.com/docker/compose/releases/latest/download/docker-compose-linux-x86_64 dest: /usr/local/bin/docker-compose mode: '0755'
Docker Swarm is another useful built-in Docker tool for more efficiently managing multiple containers across different servers. You can use Ansible to initialize Docker Swarm or join nodes to an existing one.
- name: Initialize Docker Swarm command: docker swarm init
You can also configure Docker with custom settings. For example, you might want to set a custom storage driver or data directory, or specify how the Docker daemon communicates with a Docker host. These adjustments can be made by adding a task that edits the Docker daemon configuration file, typically located at /etc/docker/daemon.json.
These are just a few ideas. The real advantage of using Ansible is that once you’ve added these tasks to your playbook, they’ll run automatically and consistently on any server you set up in the future.
Setting up Docker with Ansible isn’t just about saving time. It’s about making your setup repeatable and reliable. Instead of manually going through all the installation steps every time, you’ll now have a playbook that does it all for you.
The best part? You’re not locked into just the basics. Whether you’re managing a single VPS or preparing for a more complex setup, this approach gives you a solid and flexible foundation. You can keep building your playbook over time by updating it and testing the changes as your setup evolves.
Ansible is a tool for automating tasks – like installing software – on one or more servers. It works with Docker by handling its setup for you. Instead of running commands manually, you can just write them in a playbook and let Ansible handle the rest.
It can, with a few adjustments. This playbook is made specifically for Ubuntu, but the working principle is the same on other distributions. You’ll need to change a few commands, especially ones that deal with package installation.
You can simply list all your servers in the inventory file. Ansible will connect to each of the servers (usually over SSH) and run the playbook on each of them. That’s why Ansible is one of the easiest ways to handle installations and changes on multiple machines at once.