Docker Environment Variables
Distr allows users to define environment variable templates for Docker applications. Users can configure and customize environment variables before deploying an application.
This guide builds on top of the create a Docker application guide and explains environment variable templates in more detail.
What is an Environment Variable Template?
Section titled “What is an Environment Variable Template?”Environment variables are used to pass deployment-specific values to your application. With Distr, you can define a template for these variables, allowing users to customize them before deployment and ensuring all necessary values are set.
This setup consists of three parts:
- A parameterized Docker Compose file that references dynamic variables.
- A template file that provides default values for those variables, allowing end users to modify them in Distr before deployment.
- The actual environment configured during the deployment of the application.
Here’s an example of how it works:
The Docker Compose file will be the same for all deployments that use that specific version of the application. The environment file can be customized for each deployment.
services: web: image: '${WEB_IMAGE:?error}' container_name: '${WEB_NAME:?error}' ports: - '${WEB_PORT:?error}:80' environment: - APP_ENV=${APP_ENV:?error}WEB_IMAGE=nginx:latestWEB_NAME=my-nginx-containerWEB_PORT=8080APP_ENV=developmentYou can execute this example with the following command:
docker compose upYou can also create different environments using the --env-file flag:
docker compose --env-file .prod.env upReferencing environment variables in multiple services
Section titled “Referencing environment variables in multiple services”You need to pass the environment variables to the services in the docker-compose.yaml file.
If multiple services need the same environment variables, you can define them in x- sections of the docker-compose.yaml file.
x-shared-env: &shared-env SHARED_ENV_PRODUCT: ${SHARED_ENV_PRODUCT:?error} SHARED_ENV_DOCS_URL: ${SHARED_ENV_DOCS_URL:?error}
# Our partly shared env needs to extend the shared envx-partly-shared-env: &partly-shared-env <<: *shared-env PARTLY_SHARED_ENV: ${PARTLY_SHARED_ENV:?error}
services: one: image: 'stefanprodan/podinfo' ports: - '3001:9898' environment: <<: *partly-shared-env SERVER_NAME: ${SERVER_NAME_ONE:?error}
two: image: 'stefanprodan/podinfo' ports: - '3002:9898' environment: <<: *partly-shared-env SERVER_NAME: ${SERVER_NAME_TWO:?error}
three: image: 'stefanprodan/podinfo' ports: - '3003:9898' environment: <<: *shared-env SERVER_NAME: ${SERVER_NAME_THREE:?error}SERVER_NAME_ONE=ONESERVER_NAME_TWO=TWOSERVER_NAME_THREE=THREESHARED_ENV_PRODUCT=DistrSHARED_ENV_DOCS_URL=https://distr.sh/docsPARTLY_SHARED_ENV=ONE_AND_TWOYou can execute this example with the following command:
docker compose upWhen running the Docker Compose file you will be able to see the following output for the webservers /env endpoint:
[ "HOME=/home/app", "HOSTNAME=7278f34f08d1", "PARTLY_SHARED_ENV=ONE_AND_TWO", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "SERVER_NAME=ONE", "SHARED_ENV_DOCS_URL=https://distr.sh/docs", "SHARED_ENV_PRODUCT=Distr"][ "HOME=/home/app", "HOSTNAME=6e2a1001f79f", "PARTLY_SHARED_ENV=ONE_AND_TWO", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "SERVER_NAME=TWO", "SHARED_ENV_DOCS_URL=https://distr.sh/docs", "SHARED_ENV_PRODUCT=Distr"][ "HOME=/home/app", "HOSTNAME=b781b4ab2fe0", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "SERVER_NAME=THREE", "SHARED_ENV_DOCS_URL=https://distr.sh/docs", "SHARED_ENV_PRODUCT=Distr"]Injecting the whole environment file into a service
Section titled “Injecting the whole environment file into a service”When applying a deployment, the Distr agent writes the configured environment as a .env file next to the
docker-compose.yaml. This means you can use the standard Docker Compose
env_file directive to inject the
entire environment file into a service, instead of listing every variable individually under environment:
services: webserver: image: nginx env_file: - .env