This Ansible playbook automates the deployment of a Docker Compose application from a public GitHub repository. It performs the following steps:
- Installs Docker Engine and the Compose v2 plugin
- Clones a public GitHub repository containing a
docker-compose.yml - Creates a systemd service to automatically start the app on boot
-
Control machine with:
- Ansible ≥ 2.12 installed
- SSH access to your target host
-
Target host (Ubuntu/Debian recommended):
- Accessible via SSH
sudoprivileges- Internet access (to install Docker and clone the repo)
ansible/
├─ inventory
├─ site.yml
└─ roles/
└─ compose_app/
├─ defaults/main.yml
├─ tasks/main.yml
├─ templates/compose-app.service.j2
└─ handlers/main.yml
Define your target host(s):
[servers]
myhost ansible_host=YOUR.SERVER.IP ansible_user=YOUR_USERAdjust these variables as needed:
compose_repo_url: "https://github.com/owner/repo.git"
compose_repo_version: "main"
compose_app_dir: "/opt/compose-app"
compose_service_name: "compose-app"
compose_run_user: "{{ ansible_user | default('ubuntu') }}"
compose_file: "docker-compose.yml"
compose_env_file: ".env"
compose_env_vars: {}sudo apt update && sudo apt install -y ansible gitgit clone https://github.com/YOUR_USERNAME/ansible-compose-deploy.git
cd ansible-compose-deployEdit:
inventory→ target host & userdefaults/main.yml→ repository URL, compose directory, etc.
ansible-playbook -i inventory site.ymlsystemctl status compose-app
docker psReboot the server:
sudo rebootThen check:
docker psYour Compose app should be up and running automatically.
ansible-playbook -i inventory site.ymlThe playbook detects repo or unit changes and restarts the app automatically.
sudo systemctl restart compose-app
sudo systemctl stop compose-app
sudo systemctl disable compose-appjournalctl -u compose-app -n 100 -f- Uses Docker Compose v2 (
docker compose, notdocker-compose). - Works best on Ubuntu 20.04+ or Debian 11+.
- To use private repos, configure SSH keys and change
compose_repo_urlto an SSH URL. - You can encrypt sensitive values (like
.envvariables) with Ansible Vault.
MIT - feel free to adapt and reuse.