Sure! Here’s a rewritten version of the article you provided, keeping the meaning and structure but making it more concise, polished, and easier to read:
—
How to Install Docker on Ubuntu Using Ansible
Published: April 25, 2025
Author: Dominykas Jasiulionis
—
Manually installing Docker works well—until you have to repeat the process multiple times. That’s where Ansible comes in. With Ansible, you can automate Docker installation and ensure consistent setups across all your servers.
In this guide, you’ll learn how to use Ansible to install Docker on an Ubuntu server, from setting up Ansible to creating your first playbook.
—
What You’ll Need
Before you begin, make sure you have:
– An Ubuntu server (20.04 or newer). Check out Hostinger’s Ubuntu VPS hosting if you need one.
– Basic terminal skills — no expert knowledge required.
– A user with sudo privileges — necessary for installing packages.
– SSH access — to connect to your server.
—
Step 1: Install and Configure Ansible
First, connect to your server via SSH. Then, update your package list:
bash
sudo apt update
Next, install Ansible:
bash
sudo apt install ansible -y
Verify the installation:
bash
ansible --version
If you see version information, Ansible is ready.
—
Step 2: Create an Inventory File
Ansible uses an inventory file to know which servers to manage. Create a file named hosts.ini
:
bash
nano hosts.ini
Add the following:
ini
[local]
localhost ansible_connection=local
This tells Ansible to manage the local machine without using SSH.
**Tip:** For managing remote servers, list their IP addresses and SSH details instead.
—
Step 3: Write the Ansible Playbook
Create a playbook file called install-docker.yml
:
bash
nano install-docker.yml
Paste this content:
yaml
- name: Install Docker on Ubuntu
hosts: local
become: true
tasks:
- name: Install prerequisites
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
state: present
update_cache: true
- name: Add Docker’s 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
This playbook installs Docker and its dependencies securely and reliably.
—
Step 4: Run the Playbook
Execute the playbook with:
bash
ansible-playbook -i hosts.ini install-docker.yml
Ansible will walk through each task and apply it to your server. If Docker is already installed, Ansible will skip redundant steps automatically.
—
Step 5: Verify the Installation
Check if Docker is installed:
bash
docker --version
Check Docker’s service status:
bash
sudo systemctl status docker
You should see it marked as active (running).
Finally, test Docker by running a container:
bash
sudo docker run hello-world
A successful setup will display a “Hello from Docker!” message.
—
Step 6: Customize Your Playbook
Now that Docker is installed, you can extend your playbook. For example, to install Docker Compose, add:
yaml
- 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'
Or to initialize Docker Swarm:
yaml
- name: Initialize Docker Swarm
command: docker swarm init
You can also configure Docker settings by modifying /etc/docker/daemon.json
through Ansible.
—
Conclusion
By using Ansible to install Docker, you not only save time but also ensure a consistent and reliable setup across all your servers. Plus, your playbook can grow with your infrastructure, evolving as your needs change.
Automating your server setup is a smart investment — and with Ansible, it’s easier than ever.
—
FAQs
What is Ansible, and how
Discover more from WIREDGORILLA
Subscribe to get the latest posts sent to your email.