Interpolation (original) (raw)

A Compose file can use variables to offer more flexibility. If you want to quickly switch between image tags to test multiple versions, or want to adjust a volume source to your local environment, you don't need to edit the Compose file each time, you can just set variables that insert values into your Compose file at run time.

Interpolation can also be used to insert values into your Compose file at run time, which is then used to pass variables into your container's environment

Below is a simple example:

When you run docker compose up, the web service defined in the Compose fileinterpolates in the image webapp:v1.5 which was set in the .env file. You can verify this with theconfig command, which prints your resolved application config to the terminal:

Interpolation is applied for unquoted and double-quoted values. Both braced (${VAR}) and unbraced ($VAR) expressions are supported.

For braced expressions, the following formats are supported:

For more information, seeInterpolation in the Compose Specification.

Docker Compose can interpolate variables into your Compose file from multiple sources.

Note that when the same variable is declared by multiple sources, precedence applies:

  1. Variables from your shell environment
  2. If --env-file is not set, variables set by an .env file in local working directory (PWD)
  3. Variables from a file set by --env-file or an .env file in project directory

You can check variables and values used by Compose to interpolate the Compose model by running docker compose config --environment.

.env file

An .env file in Docker Compose is a text file used to define variables that should be made available for interpolation when running docker compose up. This file typically contains key-value pairs of variables, and it lets you centralize and manage configuration in one place. The .env file is useful if you have multiple variables you need to store.

The .env file is the default method for setting variables. 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, seeSyntax for environment files.

Basic example:

Additional information

Important

Substitution from .env files is a Docker Compose CLI feature.

It is not supported by Swarm when running docker stack deploy.

.env file syntax

The following syntax rules apply to environment files:

Substitute with --env-file

You can set default values for multiple environment variables, in an .env 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

local .env file versus .env file

An .env file can also be used to declarepre-defined environment variables used to control Compose behavior and files to be loaded.

When executed without an explicit --env-file flag, Compose searches for an .env file in your working directory (PWD) and loads values both for self-configuration and interpolation. If the values in this file define the COMPOSE_FILE pre-defined variable, which results in a project directory being set to another folder, Compose will load a second .env file, if present. This second .env file has a lower precedence.

This mechanism makes it possible to invoke an existing Compose project with a custom set of variables as overrides, without the need to pass environment variables by the command line.

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 lets you 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 previous example, 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, like postgres which defaults to the latest image, or with a tag such as postgres:15.