126
CHAPTER 5 | Designing and Developing Multi-Container and Microservice-Based .NET Applications
Using environment variables in docker-compose files
It
is convenient, especially in production environments, to be able to get configuration information
from environment variables, as we have shown in previous examples.
You can reference an
environment variable in your docker-compose files using the syntax ${MY_VAR}. The following line
from a docker-compose.prod.yml file shows how to reference the value of an environment variable.
IdentityUrl=http:
//${ESHOP_PROD_EXTERNAL_DNS_NAME_OR_IP}:5105
Environment variables are created and
initialized in different ways, depending on your host
environment (Linux, Windows,
Cloud cluster, etc.). However, a convenient approach is to use an .env
file. The docker-compose files support declaring default environment variables in the .env file. These
values for the environment variables are the default values. But they can
be overridden by the values
you might have defined in each of your environments (host OS or environment variables from your
cluster). You place this .env file in the folder where the docker-compose command is executed from.
The following example shows an .env file like the
.env
file for the eShopOnContainers application.
# .env file
ESHOP_EXTERNAL_DNS_NAME_OR_IP=
host.docker.internal
ESHOP_PROD_EXTERNAL_DNS_NAME_OR_IP=
10.121.122.92
Docker-compose expects each line in an .env file to be in
the format =.
The values set in the run-time environment always override the values defined inside the .env file. In a
similar way, values passed via command-line arguments also override the default values set in the .env
file.
Dostları ilə paylaş: