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:
- 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 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}
WEB_IMAGE=nginx:latestWEB_NAME=my-nginx-containerWEB_PORT=8080APP_ENV=development
You can execute this example with the following command:
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 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_TWO
You can execute this example with the following command:
docker compose up --env .env
When running the docker compose file you will be able to see 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"]