In this tutorial, we'll explain how to install Docker on Ubuntu using Ansible.
Ansible is a powerful automation tool that allows you to automate the process of installing and configuring software across multiple systems. In this tutorial, we'll walk you through how to use Ansible to install and set up Docker on Ubuntu.
Prerequisites
Before you begin, make sure you have the following:
- An Ubuntu dedicated server or KVM VPS (or multiple servers) where you want to install Docker.
- Ansible installed on your control machine.
- Basic Linux command knowledge.
Step 1: Install Ansible
Ubuntu builds are available in a PPA here.
To configure the PPA on your system and install Ansible run these commands (Following steps are copied from official documentation):
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
Step 2: Set Up SSH Access
We need to setup a SSH access between the remote servers to ensure you can SSH into your managed nodes without a password prompt. This can be achieved by setting up SSH keys.
Generate SSH Keys (if not already done)
On your system, generate an SSH key pair (if you don’t have one already):
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
This will create a pair of files:
- ~/.ssh/id_rsa (private key)
- ~/.ssh/id_rsa.pub (public key)
Copy the Public Key to Remote Servers
You need to copy the public key to the remote servers to enable passwordless SSH access. Use the ssh-copy-id command:
ssh-copy-id username@remote_server_ip
Replace username
with the remote user’s name and remote_server_ip
with the IP address of your remote server. Repeat this step for each remote server.
Test SSH access to ensure you can connect without a password:
ssh username@remote_server_ip
If you can connect without being prompted for a password, SSH access is correctly set up. Now exit the current connection and back to main server.
Step 3: Set Up Your Ansible Inventory
Create an inventory file to define the servers where Docker will be installed. Create a file named inventory.ini:
nano inventory.ini
Add Following content. Replace the IP addresses with your server's IP addresses.
[ubuntu_servers]
192.168.1.10
192.168.1.11
Step 4: Create an Ansible Playbook
Create a playbook file named install_docker.yml
to install Docker on your Ubuntu servers:
nano install_docker.yml
Replace {{ ansible_user }}
with your user name and add following content:
---
- hosts: ubuntu_servers
become: yes
tasks:
- name: Update and upgrade APT packages
apt:
update_cache: yes
- name: Install prerequisites
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
state: present
- name: Add Docker's official GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Set up Docker stable repository
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable
state: present
filename: docker
- name: Update APT package index after adding Docker repository
apt:
update_cache: yes
- name: Install Docker CE
apt:
name: docker-ce
state: present
- name: Start and enable Docker service
systemd:
name: docker
enabled: yes
state: started
- name: Add user to the Docker group
user:
name: "{{ ansible_user }}"
groups: docker
append: yes
Step 5: Run the Ansible Playbook
Run the playbook to install and set up Docker on your Ubuntu servers:
ansible-playbook -i inventory.ini install_docker.yml
Step 6: Verify the Docker Installation
After the playbook runs successfully, verify the Docker installation by connecting to your Ubuntu server and running:
docker --version
Output:
Docker version 27.2.0, build 3ab4256
You should see the installed Docker version displayed in the terminal.
Conclusion
We have seen how to install Docker on Ubuntu using Ansible. It is a quick and efficient way to automate the process across multiple servers. With just a few lines of code in your playbook, you can ensure that Docker is correctly installed and configured on all your target machines.