liveBook · Manning

Environment variable

As explained in Road to Kubernetes

An environment variable is a named value supplied by the execution environment to configure or provide information to a process.

An environment variable is a named value supplied to a running program. It lets you change configuration without editing application code. For example, a web server can read PORT to decide where to listen; changing the port changes how you reach the app, not its application logic. section 2.2.1 section 6.4.3

Try a different port

An Express.js app can use `const port = process.env.PORT || 3000;`. Run `PORT=3011 node main.js` on macOS or Linux, or `set PORT=3011 && node main.js` on Windows. Visit `http://127.0.0.1:3011` instead of the default port 3000. section 2.2.1 section 2.2.2

The same idea works inside a container. A Python entrypoint can set `RUNTIME_PORT=${PORT:-8080}` and bind the server to that value. The fallback keeps the app runnable when PORT is absent; supplying PORT at runtime changes its configuration without rebuilding the image. section 5.3.4 section 5.6

As deployment grows, choose where the values come from:

  • For a local Docker run, pass a value with `-e` or load an environment file with `--env-file`. Docker Compose can declare `environment` values and load a `.env` file through `env_file`. section 6.0 section 6.4.3
  • For a production Compose deployment, a workflow can create a `.env` file from GitHub Actions secrets and copy it to the virtual machine. The shown transfer does not include encryption. section 7.3.3
  • For Kubernetes, a Deployment can set values directly or obtain them from a ConfigMap or Secret. Shared resources avoid repeating values across manifests; a Secret is intended for sensitive values but is only Base64-encoded. section 8.3.5

Keep passwords and API keys out of application code, the Dockerfile, and container builds. A development `.env` file can keep them out of the Git repository, but must not be committed. Production secret-management tools are suggested rather than configured here. section 5.3.4 section 6.4.3

Listing 8.12 ConfigMap-based environment variables in a Deployment Manifest
kind: Deployment
...
spec:
  ...
  template:
    ...
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        env:
        - name: MY_ENV_VAR
          value: "This is simple"
        - name: PY_PORT
          valueFrom:
            configMapKeyRef:
              name: project-ports-cm
              key: PY_PORT
        - name: JS_PORT
          valueFrom:
            configMapKeyRef:
              name: project-ports-cm
              key: JS_PORT
        envFrom:
        - configMapRef:
            name: project-ports-cm

Compare `configMapKeyRef`, which selects a named key, with `envFrom`, which loads all keys from a ConfigMap. section 8.3.5

How Road to Kubernetes covers it

How deep it goes
Works through command-line settings, container fallbacks, environment files, and Kubernetes references; defers production secret-manager configuration.
Examples
Uses an Express.js port change, a Python container entrypoint, a Redis host setting, and Kubernetes Deployment manifests.
What it assumes
Starts with a runnable Express.js app and introduces port changes before extending the pattern to containers and Kubernetes.
What you can do after
Operate/configure applications with runtime values and select direct, file-based, or Kubernetes-supplied settings for deployments.
How it connects
Connects environment variables to Docker Compose, dotenv files, GitHub Actions secrets, Kubernetes ConfigMaps, and Kubernetes Secrets.

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 Road to Kubernetes. The section links point to the parts of the book it draws on.