Home Explore Blog Models CI



docker

1st chunk of `content/manuals/compose/how-tos/environment-variables/set-environment-variables.md`
6e3f19efca593297ceb00e174bb2b11d58eb0fa0b82206f80000000100000aa6
---
title: Set environment variables within your container's environment
linkTitle: Set environment variables
weight: 10
description: How to set, use, and manage environment variables with Compose
keywords: compose, orchestration, environment, environment variables, container environment variables
aliases:
- /compose/env/
- /compose/link-env-deprecated/
- /compose/environment-variables/set-environment-variables/
---

A container's environment is not set until there's an explicit entry in the service configuration to make this happen. With Compose, there are two ways you can set environment variables in your containers with your Compose file. 

>[!TIP]
>
> Don't use environment variables to pass sensitive information, such as passwords, in to your containers. Use [secrets](../use-secrets.md) instead.


## Use the `environment` attribute

You can set environment variables directly in your container's environment with the
[`environment` attribute](/reference/compose-file/services.md#environment) in your `compose.yaml`.

It supports both list and mapping syntax:

```yaml
services:
  webapp:
    environment:
      DEBUG: "true"
```
is equivalent to 
```yaml
services:
  webapp:
    environment:
      - DEBUG=true
```

See [`environment` attribute](/reference/compose-file/services.md#environment) 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 ...`:
  ```yaml
  web:
    environment:
      - DEBUG
  ```
The value of the `DEBUG` variable 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 the `DEBUG` variable in the shell environment is not set. 

- You can also take advantage of [interpolation](variable-interpolation.md#interpolation-syntax). In the following example, the result is similar to the one above but Compose gives you a warning if the `DEBUG` variable is not set in the shell environment or in an `.env` file in the project directory.

  ```yaml
  web:
    environment:
      - DEBUG=${DEBUG}
  ```

## Use the `env_file` attribute

A container's environment can also be set using [`.env` files](variable-interpolation.md#env-file) along with the [`env_file` attribute](/reference/compose-file/services.md#env_file).

```yaml
services:
  webapp:
    env_file: "webapp.env"
```

Using an `.env` file lets you use the same file for use by a plain `docker run --env-file ...` command, or to share the same `.env` file within multiple services without the need to duplicate a long `environment` YAML block.

Title: Setting Environment Variables in Docker Compose
Summary
This document describes how to set environment variables within Docker containers using Docker Compose. It covers two methods: using the `environment` attribute directly in the `compose.yaml` file and using the `env_file` attribute to load variables from a `.env` file. The `environment` attribute supports both list and mapping syntax and allows passing environment variables from the shell. The `env_file` attribute enables sharing the same environment file across multiple services.