This project template uses Terraform and Docker Compose to deploy a compose app with the following capabilities built in out of the box:
- automated volume and database backups using stack-back
- public access using Cloudflare Tunnel
- just-in-time secret injection using Infisical
Altogether, the template containers use <50MB of memory and <0.5GB of disk space on the container host.
This template requires pre-existing infrastructure to work. See the cjmay.dev for details.
To use this template:
- Create a new repo from this template. The repo name will become the subdomain for the app, excluding any "compose-" prefix (ex. compose-minecraft -> minecraft.yourdomain.com).
- Wait for GitHub Actions to deploy the app's infrastructure or deploy dev infrastructure manually.
- Modify
docker-compose.yamlto create a compose file with the app containers. - Make sure the
traefiklabels are set on the frontend container. - Add any additional app secrets to the deployed Infisical project.
GitHub Actions will automatically deploy the infrastructure using the provided Terraform code. These resources will be created:
- Ubuntu server with Docker installed
- Backblaze B2 bucket for backups
- Backblaze B2 app key for the bucket
- Cloudflare Tunnel to publish the app
- DNS record pointing to the tunnel
- Infisical project for app secrets
- Infisical secrets to access the above resources
After the infrastructure has been deployed, Ansible will configure the Ubuntu server and deploy the compose app on it. Infisical CLI is used to inject app secrets just-in-time, ensuring the app's secrets never touch the disk on the server** (see snippet below). Any manual work done on the Ubuntu server will also require Infisical CLI.
infisical run --env prod --command "make compose"Ansible handles updates in the same way, gracefully restarting services with Infisical CLI after the update has completed.
** Some secrets may end up in a container's volume on-disk.
To intialize a local development environment, follow these steps:
- Install Terraform and Infisical CLI tools
- Run
./local-init.sh <infra|app>and follow the prompts - Run
source .env.local
The local-init.sh script assumes you want to use the "dev" environment. If you want to use a different environment locally, make sure to update the .infisical.json file that is created during first-time setup, and then re-run local-init.sh.
Use the local init script
./local-init.sh infraYou can then deploy the infrastructure with terraform
make tf-init
make tf-plan
make tf-applyUse the local init script
./local-init.sh appYou can then configure the docker host with ansible
make configure...or deploy the compose app on the docker host
make composeMake sure to remove .env.local and log out of Infisical CLI when you are done for the day so secrets don't sit on your development machine.
make lockSome design choices were made that are helpful to know when using this template:
- The
local-init.shscript chooses the deployment environment based on the contents of.infisical.jsonwhich is initialized the first timelocal-init.shis run. - The provided
Makefilecontains various shorthand commands for working with Terraform, Ansible, and Docker Compose. Be aware of the commands and flags being used to prevent unexpected behavior. - This project uses
stack-backto backup all container volumes and databases on a default schedule. If the default backup configuration is undesireable, refer to stack-back's docs for information on configuringstack-backto fine-tune backup settings. - Git submodules are used within this template to ensure that updates to the submodules are able to easily be pulled in by repos that have cloned this template.