0Pricing
MLOps Academy · Lesson

Pass Config via Environment Variables

Keep model paths and secrets out of the image.

Pass Config via Environment Variables is a free MLOps Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MLOps Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Do Not Bake Config In

Hardcoding a model path or API key bakes it into the image forever. Pass them as environment variables at run time instead.

Read Env in Python

Use os.getenv to read a variable, with a sensible default. Your code then bends to each environment without edits.

import os
model_path = os.getenv("MODEL_PATH", "/models/latest.pkl")

Defaults Inside the Image

The Dockerfile ENV instruction sets a baked-in default. It is handy for non-secret config that rarely changes.

ENV MODEL_PATH=/models/latest.pkl

Override at Run Time

Override any value with -e on docker run. The same image then serves staging or production with different settings.

docker run -e MODEL_PATH=/models/v2.pkl model-api

Group Vars in a File

Keep many variables tidy in a file and load it with --env-file. Your run command stays short and readable.

docker run --env-file prod.env model-api

Never Commit Secrets

Keep secret files out of Git and out of the image. Add your .env to .gitignore and .dockerignore so they never leak.

Secrets Belong Outside

Real secrets like database passwords should come from a vault or your platform secret manager, injected at run time only.

Validate on Startup

Check required variables when the app boots and fail loudly if one is missing. A clear crash beats a silent wrong prediction.

if not os.getenv("MODEL_PATH"):
    raise RuntimeError("MODEL_PATH is required")

Twelve-Factor Config

The twelve-factor rule says store config in the environment. It keeps one image portable across every deployment target.

Pydantic Settings

Tools like pydantic-settings read env vars into a typed config object, validating types and defaults in one clean place.

from pydantic_settings import BaseSettings

One Image, Many Envs

The payoff: build once and run that exact image in dev, staging, and prod, changing only the variables you inject.

Quick Check

Let us check where config should live.

Recap

You read config with os.getenv, set defaults via ENV, overrode with -e and --env-file, and kept secrets out. One portable image. ✅

Frequently asked questions

Is the “Pass Config via Environment Variables” lesson free?

Yes — the full text of “Pass Config via Environment Variables” is free to read here on the web, and the MLOps Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MLOps Academy course, upgrade to CoddyKit PRO.

What will I learn in “Pass Config via Environment Variables”?

Keep model paths and secrets out of the image. You practise MLOps Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start MLOps Academy?

No prior experience is required. MLOps Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Pass Config via Environment Variables” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this MLOps Academy lesson?

Yes. Every MLOps Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Write a Dockerfile for a Model API
  2. Slim Images with Multi-Stage Builds
  3. Pass Config via Environment Variables
  4. Run and Test the Container Locally
← Back to MLOps Academy