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 up command. No additional configuration is required: simply start 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).
  • Benefit from high performance and enhanced security on private, dedicated infrastructure.
  • Apply updates with a single click for straightforward, effortless maintenance.

Your containers can be live in production in around five minutes. Stackhero manages the infrastructure, updates, and security for you, so you can focus on developing your applications. Find out more about Docker CaaS cloud hosting.

You can define environment variables in your GitHub project that will be accessible within 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).

  1. In GitHub, go to Settings > Environments and create an environment variable named WEBSITE_DOMAIN.
  2. For the staging environment, set WEBSITE_DOMAIN to "staging.my-company.com".
  3. For the production environment, set WEBSITE_DOMAIN to "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"