Skip to content

Commit 69b7b64

Browse files
committed
Add Docker installation Ansible playbook
- Create install-docker.yml playbook for automated Docker installation - Support architecture mapping (x86_64 -> amd64) for Docker repository - Include Docker service management and user group configuration - Add Docker verification with hello-world test - Update README.md with Docker installation step - Test successfully against LXD provisioned VM Features: - Installs Docker CE, CLI, containerd, and buildx plugin - Adds user to docker group for non-root usage - Starts and enables Docker service - Includes verification and testing steps - Handles Ubuntu/Debian systems with proper architecture detection
1 parent 8bb67ab commit 69b7b64

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ cd ../../ansible
9494
# Update inventory.yml with the VM's IP from step 1
9595
# Then run the verification playbook
9696
ansible-playbook wait-cloud-init.yml
97+
98+
# Install Docker on the VM
99+
ansible-playbook install-docker.yml
97100
```
98101

99102
#### 3. Verify Deployment
@@ -107,6 +110,10 @@ lxc exec torrust-vm -- /bin/bash
107110

108111
# Test SSH connection
109112
ssh -i ~/.ssh/testing_rsa torrust@<VM_IP>
113+
114+
# Verify Docker installation
115+
lxc exec torrust-vm -- docker --version
116+
lxc exec torrust-vm -- docker run --rm hello-world
110117
```
111118

112119
## 🎭 Infrastructure Workflow
@@ -140,6 +147,7 @@ Both configurations include GitHub Actions workflows for CI testing:
140147
- [x] OpenTofu infrastructure as code
141148
- [x] Ansible configuration management setup
142149
- [x] Basic cloud-init verification playbook
150+
- [x] Docker installation playbook
143151
- [x] Automated testing workflows
144152

145153
### 🔄 In Progress

config/ansible/install-docker.yml

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
# Ansible Playbook: Install Docker
3+
# This playbook installs Docker CE on Ubuntu/Debian systems
4+
#
5+
# 🔗 RELATIONSHIP WITH INFRASTRUCTURE:
6+
# 1. This playbook runs after VM provisioning (OpenTofu) and cloud-init completion
7+
# 2. It prepares the VM for running containerized applications
8+
# 3. Can be used as part of a larger deployment pipeline for Torrust applications
9+
10+
# Define which hosts this playbook will run on
11+
- name: Install Docker
12+
hosts: all # Run on all hosts defined in inventory.yml
13+
gather_facts: true # Collect system information to determine OS and version
14+
become: true # Use sudo/root privileges for system-level operations
15+
16+
# Variables that can be customized
17+
vars:
18+
docker_edition: ce # Community Edition
19+
docker_package: "docker-{{ docker_edition }}"
20+
# Map architecture to Docker repository format
21+
docker_arch: "{{ 'amd64' if ansible_architecture == 'x86_64' else ansible_architecture }}"
22+
23+
# List of tasks to execute in order
24+
tasks:
25+
# Task 1: Update package cache
26+
- name: Update apt package cache
27+
ansible.builtin.apt:
28+
update_cache: true
29+
cache_valid_time: 3600 # Cache valid for 1 hour
30+
when: ansible_os_family == "Debian"
31+
32+
# Task 2: Install required packages for Docker repository
33+
- name: Install required packages for Docker repository
34+
ansible.builtin.apt:
35+
name:
36+
- apt-transport-https
37+
- ca-certificates
38+
- curl
39+
- gnupg
40+
- lsb-release
41+
state: present
42+
when: ansible_os_family == "Debian"
43+
44+
# Task 3: Add Docker's official GPG key
45+
- name: Add Docker's official GPG key
46+
ansible.builtin.get_url:
47+
url: https://download.docker.com/linux/ubuntu/gpg
48+
dest: /etc/apt/keyrings/docker.asc
49+
mode: "0644"
50+
when: ansible_os_family == "Debian"
51+
52+
# Task 4: Add Docker repository
53+
- name: Add Docker repository
54+
ansible.builtin.apt_repository:
55+
repo: "deb [arch={{ docker_arch }} signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
56+
state: present
57+
filename: docker
58+
when: ansible_os_family == "Debian"
59+
60+
# Task 5: Update package cache after adding repository
61+
- name: Update apt package cache after adding Docker repository
62+
ansible.builtin.apt:
63+
update_cache: true
64+
when: ansible_os_family == "Debian"
65+
66+
# Task 6: Install Docker packages
67+
- name: Install Docker packages
68+
ansible.builtin.apt:
69+
name:
70+
- "{{ docker_package }}"
71+
- "{{ docker_package }}-cli"
72+
- containerd.io
73+
- docker-buildx-plugin
74+
state: present
75+
when: ansible_os_family == "Debian"
76+
77+
# Task 7: Start and enable Docker service
78+
- name: Start and enable Docker service
79+
ansible.builtin.systemd:
80+
name: docker
81+
state: started
82+
enabled: true
83+
84+
# Task 8: Add user to docker group (for non-root Docker usage)
85+
- name: Add user to docker group
86+
ansible.builtin.user:
87+
name: "{{ ansible_user }}"
88+
groups: docker
89+
append: true
90+
register: user_added_to_docker_group
91+
92+
# Task 9: Verify Docker installation
93+
- name: Verify Docker installation
94+
ansible.builtin.command: docker --version
95+
register: docker_version
96+
changed_when: false
97+
98+
# Task 10: Display Docker version
99+
- name: Display Docker version
100+
ansible.builtin.debug:
101+
msg: "{{ docker_version.stdout }}"
102+
103+
# Task 11: Test Docker with hello-world (optional verification)
104+
- name: Test Docker with hello-world container
105+
ansible.builtin.command: docker run --rm hello-world
106+
register: docker_test
107+
changed_when: false
108+
ignore_errors: true # Don't fail the playbook if this test fails
109+
110+
# Task 12: Display Docker test result
111+
- name: Display Docker test result
112+
ansible.builtin.debug:
113+
msg: "{{ docker_test.stdout }}"
114+
when: docker_test is succeeded
115+
116+
# Task 13: Warning about group membership
117+
- name: Important notice about Docker group membership
118+
ansible.builtin.debug:
119+
msg: |
120+
⚠️ IMPORTANT: User '{{ ansible_user }}' has been added to the 'docker' group.
121+
You may need to log out and log back in (or restart the session) for this change to take effect.
122+
Alternatively, you can use 'newgrp docker' to activate the group membership in the current session.
123+
when: user_added_to_docker_group is changed
124+
125+
# Handlers section - tasks that run when triggered by other tasks
126+
handlers:
127+
# Handler: Restart Docker service if needed
128+
- name: restart docker
129+
ansible.builtin.systemd:
130+
name: docker
131+
state: restarted

project-words.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
buildx
12
cloudinit
3+
containerd
24
cpus
35
dearmor
46
debootstrap
@@ -13,6 +15,7 @@ newgrp
1315
noninteractive
1416
NOPASSWD
1517
publickey
18+
pytest
1619
resolv
1720
runcmd
1821
serde

0 commit comments

Comments
 (0)