How to set environment variables in your Docker container.

Using 'docker run' or 'docker-compose.yml'

How to set environment variables in your Docker container.
Photo by Mohammad Rahmani / Unsplash

🐳 1. Using docker run

The most direct way is with the -e (or --env) flag.

Basic usage

docker run -e MY_VAR=value my-image

This sets MY_VAR=value inside the container.

Multiple variables

docker run -e MY_VAR=value -e OTHER_VAR=123 my-image

Use host environment variables

If a variable exists on your host:

export MY_VAR=value
docker run -e MY_VAR my-image

Docker will pass the value from your shell.


Use an env file

docker run --env-file .env my-image

Example .env:

MY_VAR=value
OTHER_VAR=123

🐳 2. Using docker-compose.yml

With Docker Compose, environment variables are typically defined under the environment: key.

There are two styles: map-style and list-style.


Map-style (dictionary style)

This is the most explicit and readable.

services:
  app:
    image: my-image
    environment:
      MY_VAR: value
      OTHER_VAR: "123"

Notes:

  • Keys and values are clearly separated
  • YAML parsing applies (so quoting can matter)
  • Best for clarity and maintainability

List-style (array style)

This mimics the docker run -e syntax.

services:
  app:
    image: my-image
    environment:
      - MY_VAR=value
      - OTHER_VAR=123

Notes:

  • Slightly shorter
  • No YAML key/value structure
  • Easier to copy from shell commands

Passing host variables in Compose

Works in both styles.

Map-style:

environment:
  MY_VAR: ${MY_VAR}

List-style:

environment:
  - MY_VAR=${MY_VAR}

If MY_VAR is not set, it may default to empty unless you specify:

MY_VAR: ${MY_VAR:-default_value}

🐳 3. Using a .env file with Docker Compose

This is where things get a bit subtle: Compose uses .env in two different ways.


(A) Automatic .env file (variable substitution)

If a .env file exists in the same directory as docker-compose.yml, Docker Compose will automatically load it.

Example .env:

MY_VAR=value
OTHER_VAR=123

Then in docker-compose.yml:

services:
  app:
    image: my-image
    environment:
      MY_VAR: ${MY_VAR}
      OTHER_VAR: ${OTHER_VAR}

👉 Important:

  • .env is used for substitution, not automatically injected into the container
  • You must explicitly reference variables with ${...}

(B) Using env_file (inject into container)

If you want variables directly passed into the container:

services:
  app:
    image: my-image
    env_file:
      - .env

This behaves like docker run --env-file.


Combine both (common pattern)

services:
  app:
    image: my-image
    env_file:
      - .env
    environment:
      EXTRA_VAR: something

⚠️ Key Differences (important)

  • environment: → explicitly sets variables in container
  • env_file: → loads variables from a file into container
  • .env file (auto-loaded) → used for Compose file variable substitution, not automatic injection

How to check your variables inside the container

docker exec -it <container-name> env
# or
docker exec -it <container-name> env | grep -i <var-name>

Subscribe to Michels Blog

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe