liveBook · Manning

Environment variable

As explained in Learn Docker in a Month of Lunches, Second Edition

An environment variable is a named key/value setting supplied by an operating system or execution environment to configure a process.

An environment variable is a named key/value setting supplied to a process by its execution environment. An application can read one to choose a URL, logging level, or feature setting without changing its code. Docker supplies variables to containers: an image can define defaults, and you can provide different values when creating a container. section 3.1

Change a container without changing its image

Run `docker container run --env TARGET=google.com diamol/ch03-web-ping:2e`. The web-ping application now sends requests to google.com instead of its default target. The same image supports both behaviors; end this interactive run with Ctrl-C. section 3.1

Choose where each setting belongs:

  • Set an image default with Dockerfile `ENV`, such as `ENV TARGET="blog.sixeyed.com"`. Override it when you create a container with `--env` or `-e`. section 3.2 section 3.1
  • For a Compose service, use `environment` for a setting or `env_file` to load settings from a file. Compose files can also substitute a variable such as `${TODO_WEB_PORT}` into a published-port setting. section 10.3
  • For a reusable image, combine packaged defaults with environment-specific configuration files, then use variables for targeted overrides. The application must be set up to merge those sources. section 18.0 section 18.1
Listing 10.4 The override adds configuration and a secret
services:
  todo-web:
    ports:
      - 8089:8080
    environment:
      - Database:Provider=Sqlite
    env_file:
      - ./config/logging.debug.env

secrets:
  todo-db-connection:
    file: ./config/empty.json

This Compose override shows a setting under `environment`, an `env_file` for logging settings, and a separate file-backed secret. section 10.3

Check the result rather than assuming an override took effect. For web-ping, compare its request output before and after changing `TARGET`. Other applications expose configuration through an API or show its effects in logs. If an older application cannot read environment variables, a startup utility can merge variable and file overrides and write the configuration file it expects. section 3.1 section 18.4

The production payoff is one image promoted through test and production with different settings supplied at deployment. Variables are especially useful for quick feature-level changes, while environment-level overrides commonly come from files. section 18.5

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Defines key/value settings and works through Dockerfile defaults, runtime overrides, Compose injection, configuration precedence, and a legacy-app adapter.
Examples
Uses a web-ping target override, Compose service settings, and environment selection for a .NET application.
What it assumes
Starts with a key/value definition while assuming basic ability to run Docker containers and read simple configuration snippets.
What you can do after
Operate/configure it with Docker `--env`, Dockerfile `ENV`, or Compose settings, then check the resulting application behavior.
How it connects
Connects Docker images, Dockerfiles, Docker Compose, configuration files, secrets, and CI/CD pipelines.

Also explained in

Compare how 19 books explain environment variable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Docker in a Month of Lunches, Second Edition. The section links point to the parts of the book it draws on.