Docker: Using environment variables in docker-compose.yml
This documentation is part of the Deploy with GitHub Actions guide. You can view the complete guide here: Learn how to deploy your Docker containers using GitHub Actions.
Welcome to the Stackhero documentation
Stackhero offers a ready-to-use Docker cloud CaaS (Containers as a Service) platform, designed for fast and reliable container deployments:
- Deploy to production with a single
docker-compose upcommand. No additional configuration required: just launch your containers.- Choose your own domain name, automatically secured with HTTPS (for example: https://api.your-company.com, https://www.your-company.com, or https://backoffice.your-company.com).
- Take advantage of high performance and enhanced security on private, dedicated infrastructure.
- Apply updates with a single click for simple, effortless maintenance.
Your containers can be running in production in about five minutes. Stackhero manages the infrastructure, updates, and security for you, so you can focus on developing your applications. Learn more about Docker CaaS cloud hosting.
You can define environment variables in your GitHub project that will be accessible in your docker-compose.yml file. This is very useful if you want to deploy to different domains (for example, staging.my-company.com for staging and my-company.com for production).
- In GitHub, go to Settings > Environments and create an environment variable named
WEBSITE_DOMAIN. - For the staging environment, set
WEBSITE_DOMAINto "staging.my-company.com". - For the production environment, set
WEBSITE_DOMAINto "my-company.com".
Next, update your .github/workflows/deploy-to-stackhero.yml file to pass the WEBSITE_DOMAIN variable to the Stackhero GitHub Action:
name: Deploy to Stackhero
description: Deploy branch "${{ github.ref_name }}" to Stackhero
on:
push:
branches: [ "production", "staging" ]
jobs:
Deploy:
environment: ${{ github.ref_name }}
runs-on: ubuntu-latest
steps:
- uses: stackhero-io/github-actions-deploy-docker-containers-to-stackhero@v1
with:
certificates_password: ${{ secrets.STACKHERO_CERTIFICATES_PASSWORD }}
endpoint: ${{ vars.STACKHERO_ENDPOINT }}
deployment_command: ${{ vars.STACKHERO_DEPLOY_COMMAND }}
env:
WEBSITE_DOMAIN: ${{ vars.WEBSITE_DOMAIN }}
Then update your docker-compose.yml file to use the WEBSITE_DOMAIN variable instead of a hard-coded domain name:
services:
test:
image: nginx
labels:
- "traefik.enable=true"
- "traefik.http.routers.test.rule=Host(`${WEBSITE_DOMAIN}`)" # Uses the WEBSITE_DOMAIN environment variable as the domain name.
- "traefik.http.routers.test.tls.certresolver=letsencrypt"