Skip to content

Commit 69c0073

Browse files
committed
Add Docker Compose installation Ansible playbook
- Create install-docker-compose.yml playbook for automated Docker Compose installation - Include Docker dependency check to ensure Docker is installed first - Support architecture mapping for Docker Compose binary downloads - Add comprehensive testing with docker-compose config validation - Update README.md with Docker Compose installation step - Test successfully against LXD provisioned VM Features: - Downloads latest Docker Compose v2.29.7 binary - Creates system-wide symlinks for easy access - Validates installation with version check and compose file test - Includes cleanup of test resources - Provides detailed installation summary - Handles existing installations gracefully
1 parent 69b7b64 commit 69c0073

File tree

2 files changed

+158
-1
lines changed

2 files changed

+158
-1
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ ansible-playbook wait-cloud-init.yml
9797

9898
# Install Docker on the VM
9999
ansible-playbook install-docker.yml
100+
101+
# Install Docker Compose on the VM (optional)
102+
ansible-playbook install-docker-compose.yml
100103
```
101104

102105
#### 3. Verify Deployment
@@ -114,6 +117,9 @@ ssh -i ~/.ssh/testing_rsa torrust@<VM_IP>
114117
# Verify Docker installation
115118
lxc exec torrust-vm -- docker --version
116119
lxc exec torrust-vm -- docker run --rm hello-world
120+
121+
# Verify Docker Compose installation (if installed)
122+
lxc exec torrust-vm -- docker-compose --version
117123
```
118124

119125
## 🎭 Infrastructure Workflow
@@ -148,12 +154,12 @@ Both configurations include GitHub Actions workflows for CI testing:
148154
- [x] Ansible configuration management setup
149155
- [x] Basic cloud-init verification playbook
150156
- [x] Docker installation playbook
157+
- [x] Docker Compose installation playbook
151158
- [x] Automated testing workflows
152159

153160
### 🔄 In Progress
154161

155162
- [ ] Extended Ansible playbooks for application deployment
156-
- [ ] Docker Compose integration testing
157163
- [ ] Performance benchmarking
158164
- [ ] Official GitHub Actions nested virtualization clarification
159165

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
# Ansible Playbook: Install Docker Compose
3+
# This playbook installs Docker Compose on Ubuntu/Debian systems
4+
#
5+
# 🔗 RELATIONSHIP WITH INFRASTRUCTURE:
6+
# 1. This playbook runs after VM provisioning (OpenTofu) and cloud-init completion
7+
# 2. This should be run after the Docker installation playbook (install-docker.yml)
8+
# 3. It prepares the VM for running multi-container applications with docker-compose
9+
10+
# Define which hosts this playbook will run on
11+
- name: Install Docker Compose
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_compose_version: "2.29.7" # Latest stable version as of Sept 2025
19+
# Map architecture to Docker Compose binary format
20+
docker_compose_arch: "{{ 'x86_64' if ansible_architecture == 'x86_64' else ansible_architecture }}"
21+
docker_compose_url: "https://github.com/docker/compose/releases/download/v{{ docker_compose_version }}/docker-compose-linux-{{ docker_compose_arch }}"
22+
23+
# List of tasks to execute in order
24+
tasks:
25+
# Task 1: Check if Docker is installed
26+
- name: Check if Docker is installed
27+
ansible.builtin.command: docker --version
28+
register: docker_check
29+
failed_when: false
30+
changed_when: false
31+
32+
# Task 2: Fail if Docker is not installed
33+
- name: Ensure Docker is installed before proceeding
34+
ansible.builtin.fail:
35+
msg: "Docker is not installed. Please run the install-docker.yml playbook first."
36+
when: docker_check.rc != 0
37+
38+
# Task 3: Display Docker version for confirmation
39+
- name: Display current Docker version
40+
ansible.builtin.debug:
41+
msg: "Docker is installed: {{ docker_check.stdout }}"
42+
when: docker_check.rc == 0
43+
44+
# Task 4: Check if Docker Compose is already installed
45+
- name: Check if Docker Compose is already installed
46+
ansible.builtin.command: docker-compose --version
47+
register: docker_compose_check
48+
failed_when: false
49+
changed_when: false
50+
51+
# Task 5: Display current Docker Compose version if already installed
52+
- name: Display current Docker Compose version
53+
ansible.builtin.debug:
54+
msg: "Docker Compose already installed: {{ docker_compose_check.stdout }}"
55+
when: docker_compose_check.rc == 0
56+
57+
# Task 6: Create keyrings directory if it doesn't exist
58+
- name: Create /usr/local/bin directory
59+
ansible.builtin.file:
60+
path: /usr/local/bin
61+
state: directory
62+
mode: '0755'
63+
64+
# Task 7: Download Docker Compose binary
65+
- name: Download Docker Compose binary
66+
ansible.builtin.get_url:
67+
url: "{{ docker_compose_url }}"
68+
dest: /usr/local/bin/docker-compose
69+
mode: '0755'
70+
owner: root
71+
group: root
72+
force: true # Overwrite if exists to ensure we have the specified version
73+
register: docker_compose_download
74+
75+
# Task 8: Create symbolic link for system-wide access
76+
- name: Create symbolic link for docker-compose
77+
ansible.builtin.file:
78+
src: /usr/local/bin/docker-compose
79+
dest: /usr/bin/docker-compose
80+
state: link
81+
force: true # Overwrite existing symlink if present
82+
83+
# Task 9: Verify Docker Compose installation
84+
- name: Verify Docker Compose installation
85+
ansible.builtin.command: docker-compose --version
86+
register: docker_compose_version_output
87+
changed_when: false
88+
89+
# Task 10: Display Docker Compose version
90+
- name: Display Docker Compose version
91+
ansible.builtin.debug:
92+
msg: "{{ docker_compose_version_output.stdout }}"
93+
94+
# Task 11: Test Docker Compose with a simple compose file
95+
- name: Create test docker-compose.yml file
96+
ansible.builtin.copy:
97+
content: |
98+
version: '3.8'
99+
services:
100+
hello-world:
101+
image: hello-world
102+
container_name: compose-test-hello
103+
dest: /tmp/test-docker-compose.yml
104+
mode: '0644'
105+
106+
# Task 12: Test Docker Compose functionality
107+
- name: Test Docker Compose with hello-world
108+
ansible.builtin.command: docker-compose -f /tmp/test-docker-compose.yml up --no-start
109+
register: docker_compose_test
110+
changed_when: false
111+
ignore_errors: true # Don't fail the playbook if this test fails
112+
113+
# Task 13: Clean up test compose file and containers
114+
- name: Clean up test docker-compose resources
115+
ansible.builtin.command: docker-compose -f /tmp/test-docker-compose.yml down --remove-orphans
116+
ignore_errors: true
117+
changed_when: false
118+
119+
# Task 14: Remove test compose file
120+
- name: Remove test docker-compose.yml file
121+
ansible.builtin.file:
122+
path: /tmp/test-docker-compose.yml
123+
state: absent
124+
125+
# Task 15: Display Docker Compose test result
126+
- name: Display Docker Compose test result
127+
ansible.builtin.debug:
128+
msg: "Docker Compose test completed successfully"
129+
when: docker_compose_test.rc == 0
130+
131+
# Task 16: Display installation summary
132+
- name: Installation summary
133+
ansible.builtin.debug:
134+
msg: |
135+
✅ Docker Compose installation completed!
136+
📦 Version: {{ docker_compose_version }}
137+
📍 Binary location: /usr/local/bin/docker-compose
138+
🔗 Symlink: /usr/bin/docker-compose
139+
🏠 Architecture: {{ docker_compose_arch }}
140+
141+
You can now use Docker Compose to manage multi-container applications:
142+
- docker-compose --version
143+
- docker-compose up
144+
- docker-compose down
145+
146+
# Handlers section - tasks that run when triggered by other tasks
147+
handlers:
148+
# Handler: No specific handlers needed for Docker Compose installation
149+
- name: docker-compose installed
150+
ansible.builtin.debug:
151+
msg: "Docker Compose has been installed successfully"

0 commit comments

Comments
 (0)