Skip to content

Configure 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.

In this guide, builds on top the onboard a Docker application and explains the environment variable templates in more detail.

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:

  1. A parameterized Docker Compose file that references dynamic variables.
  2. A template file that provides default values for those variables, allowing end users to modify them in Distr before deployment.
  3. 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 kustomize 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}

You can execute this example with the following command:

Terminal window
docker compose up --env .env

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 env
x-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}

You can execute this example with the following command:

Terminal window
docker compose up --env .env

When running the docker compose file you will be able to see following output for the webservers /env endpoint:

http://localhost:3001/env
[
"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"
]