There are two sets of separate and independent environment variables to consider in Docker...
- Local environment variables in the shell where you are calling the docker/docker-compose commands, like your laptop, CI/CD, or production server)
- Container environment variables in your running containers
There are two basic concepts when using environment variables in Docker...
- Environment variable substitution in your docker-compose files
- Setting the container environment variables in docker and docker-compose
Documentation: https://docs.docker.com/compose/compose-file/compose-file-v3/#variable-substitution
-
Both
$VARIABLEand${VARIABLE}syntax can be used -
Environment variable (
.env) files can be used both to set values for any defined environment variables in your containers and to configure docker-compose settings -
To match the exact state of the local environment variable (i.e. not set, set but empty, set with value), use the raw environment variable on the "left hand side", for example in
.env...TAGOr for example in setting container environment variables in
docker-compose.yml...version: '3.4' services: targetapp: image: target-app:${TAG} environment: - DB_NAME
-
Docker supports the typical *nix shell default values syntax:
This Variable Substitution... Sets the default value defaultIF...${ENVIRONMENTVARIABLE-default}ENVIRONMENTVARIABLEis Not Set${ENVIRONMENTVARIABLE:-default}ENVIRONMENTVARIABLEis Not Set or Empty -
Docker supports the typical *nix shell variable error syntax:
This Variable Substitution... Exits with an Error Message errIF...${ENVIRONMENTVARIABLE?err}ENVIRONMENTVARIABLEis Not Set${ENVIRONMENTVARIABLE:?err}ENVIRONMENTVARIABLEis Not Set or Empty
Documentation: https://docs.docker.com/compose/environment-variables/#the-env-file
- By default, docker-compose will look for a file named
.envin the directory where you are running the docker-compose command
Documentation: https://docs.docker.com/compose/environment-variables/#using-the---env-file--option
-
Specify a different environment variable file using the
--env-fileoption with the docker-compose commands, for example...% docker-compose --env-file .env.staging config -
Specify an environment file in
docker-compose.ymlfile using theenv_filekey under the service, for example...version: '3.4' services: targetapp: image: target-app:${TAG} environment: - DB_NAME env_file: .env.staging
-
Local environment variables take precedence over those in
.envor specified by--env-file
Documentation: https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file
-
Use the
docker run -e(ordocker run --env) option to set an environment variable in the container, for example...% docker run -e MY_USERNAME=tomsmith --env MY_PASSWORD=SuperSecretPassword! target-app -
Use local environment variable substitution, for example...
% DB_NAME=Sales docker run --env DB_NAME target-app -
Use the
docker run --env-fileoption to specify a file to set the container's environment variables, for example...% docker run --env-file .env target-app
Documentation: https://docs.docker.com/compose/environment-variables/
-
Use the
docker-compose run -eoption to set container environment variables on the command line in docker-compose, for example...% docker-compose run -e MY_USERNAME=tomsmith -e MY_PASSWORD=SuperSecretPassword! targetapp -
Use local environment variable substitution, for example...
% DB_NAME=Sales docker-compose run -e DB_NAME target-app -
Use the
environmentkey under your service in thedocker-compose.ymlfile to set your container environment variables, for example...version: '3.4' services: targetapp: image: target-app:${TAG} environment: - MY_USERNAME=tomsmith - MY_PASSWORD
-
Use the
env_filekey under your service in thedocker-compose.ymlfile to specify an environment file to set your container environment variables, for example...version: '3.4' services: targetapp: image: target-app:${TAG} environment: - DB_NAME env_file: .env.staging
-
Here is the order of precedence for setting container environment variables in docker-compose...
- Environment variable settings in the docker-compose file
- Local environment variables
- Environment file
- Dockerfile