Docker: Using environment variables in docker-compose.yml

This documentation is part of the Deploy with GitHub Actions guide. View the full guide here: Learn how to deploy your Docker containers using GitHub Actions.

Welcome to the Stackhero documentation

Stackhero provides 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. There is no extra setup: just start your containers.
  • Pick your own domain name, secured automatically with HTTPS (for example: https://api.your-company.com, https://www.your-company.com, or https://backoffice.your-company.com).
  • Benefit from strong performance and robust security on private, dedicated infrastructure.
  • Apply updates with one click for simple, low-effort maintenance.

You can have your containers live in production in about five minutes. Stackhero manages the infrastructure, updates, and security for you, so you can focus on delivering your applications. Read more about Docker CaaS cloud hosting.

You can define environment variables in your GitHub project that will be accessible inside your docker-compose.yml file. This is very helpful 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 }}

Now, 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"