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 offers a ready-to-use Docker cloud CaaS (Containers as a Service) solution that provides a host of benefits, including:
- Easily deploy your containers to production with just a
docker-compose up.- Customizable domain name secured with HTTPS (for example, https://api.your-company.com, https://www.your-company.com, https://backoffice.your-company.com).
- Optimal performance and robust security powered by a private and dedicated VM.
- Effortless updates with just a click.
Save time and simplify your life: it only takes 5 minutes to try Stackhero's Docker CaaS cloud hosting solution and deploy your containers to production!
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).
- 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 }}
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"