1st chunk of `content/manuals/compose/how-tos/environment-variables/envvars-precedence.md`
7370c71402d7f02a62d615bf2188de68db5d2bc7f2b241c40000000100000fbd
---
title: Environment variables precedence in Docker Compose
linkTitle: Environment variables precedence
description: Scenario overview illustrating how environment variables are resolved
in Compose
keywords: compose, environment, env file
weight: 20
aliases:
- /compose/envvars-precedence/
- /compose/environment-variables/envvars-precedence/
---
When the same environment variable is set in multiple sources, Docker Compose follows a precedence rule to determine the value for that variable in your container's environment.
This page contains information on the level of precedence each method of setting environmental variables takes.
The order of precedence (highest to lowest) is as follows:
1. Set using [`docker compose run -e` in the CLI](set-environment-variables.md#set-environment-variables-with-docker-compose-run---env).
2. Set with either the `environment` or `env_file` attribute but with the value interpolated from your [shell](variable-interpolation.md#substitute-from-the-shell) or an environment file. (either your default [`.env` file](variable-interpolation.md#env-file), or with the [`--env-file` argument](variable-interpolation.md#substitute-with---env-file) in the CLI).
3. Set using just the [`environment` attribute](set-environment-variables.md#use-the-environment-attribute) in the Compose file.
4. Use of the [`env_file` attribute](set-environment-variables.md#use-the-env_file-attribute) in the Compose file.
5. Set in a container image in the [ENV directive](/reference/dockerfile.md#env).
Having any `ARG` or `ENV` setting in a `Dockerfile` evaluates only if there is no Docker Compose entry for `environment`, `env_file` or `run --env`.
## Simple example
In the following example, a different value for the same environment variable in an `.env` file and with the `environment` attribute in the Compose file:
```console
$ cat ./webapp.env
NODE_ENV=test
$ cat compose.yaml
services:
webapp:
image: 'webapp'
env_file:
- ./webapp.env
environment:
- NODE_ENV=production
```
The environment variable defined with the `environment` attribute takes precedence.
```console
$ docker compose run webapp env | grep NODE_ENV
NODE_ENV=production
```
## Advanced example
The following table uses `VALUE`, an environment variable defining the version for an image, as an example.
### How the table works
Each column represents a context from where you can set a value, or substitute in a value for `VALUE`.
The columns `Host OS environment` and `.env` file is listed only for illustration purposes. In reality, they don't result in a variable in the container by itself, but in conjunction with either the `environment` or `env_file` attribute.
Each row represents a combination of contexts where `VALUE` is set, substituted, or both. The **Result** column indicates the final value for `VALUE` in each scenario.
| # | `docker compose run` | `environment` attribute | `env_file` attribute | Image `ENV` | `Host OS` environment | `.env` file | | Result |
|:--:|:----------------:|:-------------------------------:|:----------------------:|:------------:|:-----------------------:|:-----------------:|:---:|:----------:|
| 1 | - | - | - | - | `VALUE=1.4` | `VALUE=1.3` || - |
| 2 | - | - | `VALUE=1.6` | `VALUE=1.5` | `VALUE=1.4` | - ||**`VALUE=1.6`** |
| 3 | - | `VALUE=1.7` | - | `VALUE=1.5` | `VALUE=1.4` | - ||**`VALUE=1.7`** |
| 4 | - | - | - | `VALUE=1.5` | `VALUE=1.4` | `VALUE=1.3` ||**`VALUE=1.5`** |
| 5 |`--env VALUE=1.8` | - | - | `VALUE=1.5` | `VALUE=1.4` | `VALUE=1.3` ||**`VALUE=1.8`** |