Ways to Set Environment Variables With Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define the services that make up your application, the networks they connect to, and the environment variables they need to run.
With Docker Compose, there are multiple ways you can set environment variables in your containers. You can use either your Compose file, or the CLI.
Be aware that each method is subject to environment variable precedence.
Tip
Don’t use environment variables to pass sensitive information, such as passwords, in to your containers. Use secrets instead.
Compose file
Substitute with an .env file
An .env file in Docker Compose is a text file used to define environment variables that should be made available to Docker containers when running docker compose up. This file typically contains key-value pairs of environment variables, and it allows you to centralize and manage configuration in one place. The .env file is useful if you have multiple environment variables you need to store.
The .env file is the default method for setting environment variables in your containers. The .env file should be placed at the root of the project directory next to your compose.yaml file. For more information on formatting an environment file, see Syntax for environment files.
Below is a simple example:
|
|
When you run docker compose up, the web service defined in the Compose file interpolates in the image webapp:v1.5 which was set in the .env file. You can verify this with the config command, which prints your resolved application config to the terminal:
|
|
Additional information
-
As of Docker Compose version 2.24.0, you can set your
.envfile to be optional by using theenv_fileattribute. Whenrequiredis set tofalseand the.envfile is missing, Compose silently ignores the entry.1 2 3 4 5env_file: - path: ./default.env required: true # default - path: ./override.env required: false -
If you define an environment variable in your
.envfile, you can reference it directly in yourcompose.ymlwith theenvironmentattribute. For example, if your.envfile contains the environment variableDEBUG=1and yourcompose.ymlfile looks like this:1 2 3 4 5services: webapp: image: my-webapp-image environment: - DEBUG=${DEBUG}Docker Compose replaces
${DEBUG}with the value from the.envfile -
You can use multiple
.envfiles in yourcompose.ymlwith theenv_fileattribute, and Docker Compose reads them in the order specified. If the same variable is defined in multiple files, the last definition takes precedence:1 2 3 4 5 6services: webapp: image: my-webapp-image env_file: - .env - .env.override -
You can place your
.envfile in a location other than the root of your project’s directory, and then use one of the following methods so Compose can navigate to it: -
Values in your
.envfile can be overridden from the command line by usingdocker-compose up -e. -
Your
.envfile can be overridden by another.envif it is substituted with--env-file.
Important
Substitution from
.envfiles is a Docker Compose CLI feature.It is not supported by Swarm when running
docker stack deploy.
Use the environment attribute
You can set environment variables directly in your Compose file without using an .env file, with the environment attribute in your compose.yml. It works in the same way as docker run -e VARIABLE=VALUE ...
|
|
See environment attribute for more examples on how to use it.
Additional information
-
You can choose not to set a value and pass the environment variables from your shell straight through to your containers. It works in the same way as
docker run -e VARIABLE ...:1 2 3web: environment: - DEBUGThe value of the
DEBUGvariable in the container is taken from the value for the same variable in the shell in which Compose is run. Note that in this case no warning is issued if theDEBUGvariable in the shell environment is not set. -
You can also take advantage of interpolation.
1 2 3web: environment: - DEBUG=${DEBUG}The result is similar to the one above but Compose gives you a warning if the
DEBUGvariable is not set in the shell environment.
Use the env_file attribute
The env_file attribute lets you use multiple .env files in your Compose application. It also helps you keep your environment variables separate from your main configuration file, providing a more organized and secure way to manage sensitive information, as you do not need to place your .env file in the root of your project’s directory.
It works in the same way as docker run --env-file=FILE ....
|
|
Additional information
-
If multiple files are specified, they are evaluated in order and can override values set in previous files.
-
Environment variables declared in the
.envfile cannot then be referenced again separately in the Compose file. -
If you use both the
env_fileandenvironmentattribute, environment variables set byenvironmenttake precedence. -
The paths to your
.envfile, specified in theenv_fileattribute, are relative to the location of yourcompose.ymlfile. -
Values in your
.envfiles can be overridden from the command line by usingdocker compose run -e. -
Your
.envfiles can be overriden by another.envif it is substituted with--env-file. -
As of Docker Compose version 2.24.0, you can set your
.envfile to be optional by using therequiredfield. Whenrequiredis set tofalseand the.envfile is missing, Compose silently ignores the entry.1 2 3 4 5env_file: - path: ./default.env required: true # default - path: ./override.env required: false
Substitute from the shell
You can use existing environment variables from your host machine or from the shell environment where you execute docker compose commands. This allows you to dynamically inject values into your Docker Compose configuration at runtime.
For example, suppose the shell contains POSTGRES_VERSION=9.3 and you supply the following configuration:
|
|
When you run docker compose up with this configuration, Compose looks for the POSTGRES_VERSION environment variable in the shell and substitutes its value in. For this example, Compose resolves the image to postgres:9.3 before running the configuration.
If an environment variable is not set, Compose substitutes with an empty string. In the example above, if POSTGRES_VERSION is not set, the value for the image option is postgres:.
Note
postgres:is not a valid image reference. Docker expects either a reference without a tag, likepostgreswhich defaults to the latest image, or with a tag such aspostgres:15.
Important
Values set in the shell environment override those set in the
.envfile, theenvironmentattribute, and theenv_fileattribute. For more information, see Environment variable precedence.
CLI
Substitute with –env-file
You can set default values for multiple environment variables, in an environment file and then pass the file as an argument in the CLI.
The advantage of this method is that you can store the file anywhere and name it appropriately, for example,
This file path is relative to the current working directory where the Docker Compose command is executed. Passing the file path is done using the --env-file option:
|
|
Additional information
-
This method is useful if you want to temporarily override an
.envfile that is already referenced in yourcompose.ymlfile. For example you may have different.envfiles for production (.env.prod) and testing (.env.test). In the following example, there are two environment files,.envand.env.dev. Both have different values set forTAG.1 2 3 4 5 6 7 8 9$ cat .env TAG=v1.5 $ cat ./config/.env.dev TAG=v1.6 $ cat compose.yml services: web: image: "webapp:${TAG}"If the
--env-fileis not used in the command line, the.envfile is loaded by default:1 2 3$ docker compose config services: web: image: 'webapp:v1.5'Passing the
--env-fileargument overrides the default file path:1 2 3$ docker compose --env-file ./config/.env.dev config services: web: image: 'webapp:v1.6'When an invalid file path is being passed as an
--env-fileargument, Compose returns an error:1 2$ docker compose --env-file ./doesnotexist/.env.dev config ERROR: Couldn't find env file: /home/user/./doesnotexist/.env.dev -
You can use multiple
--env-fileoptions to specify multiple environment files, and Docker Compose reads them in order. Later files can override variables from earlier files.1$ docker compose --env-file .env --env-file .env.override up -
You can override specific environment variables from the command line when starting containers.
1$ docker compose --env-file .env.dev up -e DATABASE_URL=mysql://new_user:new_password@new_db:3306/new_database
Set environment variables with docker compose run –env
Similar to docker run --env, you can set environment variables temporarily with docker compose run --env or its short form docker compose run -e:
|
|
Additional information
-
You can also pass a variable from the shell by not giving it a value:
1$ docker compose run -e DEBUG web python console.pyThe value of the
DEBUGvariable in the container is taken from the value for the same variable in the shell in which Compose is run.
Further resources
- Understand environment variable precedence.
- Set or change predefined environment variables
- Explore best practices
- Understand the syntax and formatting guidelines for environment files