Skip to main content
This guide shows a simple, configurable Flask web application that reads its background color from an environment variable instead of hard-coding it. Using environment variables for configuration keeps deployment and application concerns separated and makes it easy to change behavior across environments without modifying code. The sample app is based on the Python Basics course.

Hard-coded color (initial example)

A typical Flask app that hard-codes the color looks like this:
Hard-coding values like this requires changing application code whenever you change configuration. Moving configuration into environment variables allows changing behavior at runtime or deployment time instead.

Read color from an environment variable

Update the app to read from an environment variable named APP_COLOR, with a sensible default:
This small change decouples configuration from code and works the same in local development and inside containers.

Run locally with different colors

You can set the environment variable before running, or inline for a single command:

Build and run in Docker

When you package the application into a Docker image (for example, simple-webapp-color), pass the environment variable into the container at runtime using -e:
To run multiple containers with different colors, start multiple containers and provide a different APP_COLOR value for each docker run invocation.

Quick reference: common commands

Using environment variables for configuration keeps code and deployment concerns separate and makes it easy to change behavior across environments without modifying the application.

Inspecting environment variables in a container

To see which environment variables are set inside a running container, use docker inspect. The container properties include a Config.Env section that lists environment variables. A full docker inspect <container-id> will include JSON like this:
For a compact view of only the environment variables, use:
Note: wrap the template {{json .Config.Env}} in backticks when including it in MDX or other contexts that may interpret curly braces.

Why environment variables?

  • Simple and language-agnostic: Most runtimes expose environment variables easily.
  • Secure for secrets when used with secret managers (avoid committing secrets to source code).
  • Works well with containers and orchestration platforms (Docker, Kubernetes, etc.).
  • Allows different settings per environment without code changes.

Watch Video

Practice Lab