EPRM-L03: Containerization and Software Engineering Processes

In short

How do you get code that works on your laptop to run the same way everywhere, and how do you stop a team’s mistakes from reaching real users? Machines differ in architecture, operating system, configuration and packages, so even a one-line hello world can break elsewhere. A container packs the application with everything it needs and runs it isolated on the host’s operating system, which makes it far lighter than a virtual machine. An ML application also talks to databases, MLflow and cloud storage, which mocked unit tests cannot check; integration tests can, with each dependency in its own container, wired together by Docker Compose. Mistakes are inevitable, so processes keep them out of production: development, acceptance and production environments, GitFlow branches mapped onto them, reviewed pull requests, and technical debt taken on only deliberately.

There is no written exam: this feeds Assignment 3 (debugging a Docker container, and adding SQL through Docker Compose), the course project and the TA questioning about your code.

Software needs a package

How do you ship a product from the Netherlands to the US? Whatever the vehicle, you package it. The package’s core job is to separate the product from the shipping process: the carrier does not need to know what is inside, it takes the box and moves it to the address on the label.

Software needs the same mechanism. You want to package your code and ship it to whatever the end point is: a bare-metal server, a cloud compute instance, a phone. Software deployment is much harder than shipping a box, and containerization is the latest answer.

”It works on my machine”

There is no single computer. Machines of the same brand run different operating systems, different versions of them, set up in different ways, and all of that changes how programs run. You might think this only matters for big, complex code. It does not.

The hello world example. The lecturer, Kemal Yesilbek, ran the same line in two containers, one with Python 2.7 and one with Python 3.13:

$ docker run -it --entrypoint /bin/bash python:2.7.11-slim
root@5fa35c9439e2:/# python --version
Python 2.7.11
root@5fa35c9439e2:/# python -c "print('hello', flush=True)"
  File "<string>", line 1
    print('hello', flush=True)
                        ^
SyntaxError: invalid syntax
root@5fa35c9439e2:/# python -c "print('hello')"
hello
 
$ docker run -it --entrypoint /bin/bash python:3-slim
root@58919815cced:/# python --version
Python 3.13.5
root@58919815cced:/# python -c "print('hello', flush=True)"
hello

The flush keyword tells Python to push the text out of its buffer immediately, so it appears on the terminal at once. It was introduced in Python 3.3. A colleague on a newer Python writes perfectly legitimate code with it, and on your older interpreter that code does not run. (From outside the course material: the error on 2.7 is a syntax error because in Python 2 print is a statement, so print('hello') only works because the brackets are read as grouping one value.)

That example is small, but realistic. Differences can sit at every level:

Architecture  ->  OS and version  ->  OS configuration  ->  Packages and versions
(x86_64, ARM)     (Windows, Linux)    (settings, setup)     (dependencies)

Each level can change how the code executes and what it returns. A real case from NumPy’s issue tracker (issue 21209, “float32 differences between x86_64 (intel) and arm64 (M1 pro)”): the same Python, pandas and NumPy versions (1.22.3) gave different float32 results on Intel and Apple M1 machines, while float64 results matched. Same code, same packages, different hardware, different numbers.

So how do you make sure code runs everywhere, runs the same and gives the same result? One way is to make your laptop the server and ship your laptop. The meme version: “It works on my machine.” “Then we’ll ship your machine.” “And that is how Docker was born.”

From servers to virtual machines to containers

You have an app and want to bring it to the live environment. There have been three ways of doing it.

Way beforeVirtual machinesContainers
Obtain serversObtain serversObtain servers
Configure serversSelect a hypervisorSelect an orchestrator
Move code onto the serverSelect a VM typeCreate containers
Install dependenciesCreate imagesPush them to a registry
Spin up the appDeploy the VM on the serverDeploy to the orchestrator
Check resources constantlyIt brings the entire OS with itForget about servers
You had Linux version A, the server BMesses up your machine, weird memory errorsRuns anywhere exactly the same

A hypervisor is the software that runs virtual machines on a physical machine. An orchestrator is the system that runs containers on a pool of machines and keeps them running (Kubernetes, in week 6). A registry is a store of container images that servers pull from, like a package index for whole applications. (Strictly, what you push is the container’s image, explained below.)

A virtual machine solves the same problem as a container, by packing a whole computer. The main difference: VMs are heavy because each brings its own operating system; containers are light because they run on the host’s existing operating system. VMs therefore waste resources: instead of every app bringing its own OS, containers share one. (From outside the course material: a VM also takes minutes to boot, a container seconds.) Isolation is the other gain: one container is one application instance, neatly separated from the rest.

  Virtual machines                    Containers
+-------+-------+-------+     +-----+-----+-----+-----+
| App A | App B | App C |     |App A|App B|App C|App D|
| libs  | libs  | libs  |     |libs |libs |libs |libs |
|Guest  |Guest  |Guest  |     +-----+-----+-----+-----+
|  OS   |  OS   |  OS   |     |   Container engine    |
+-------+-------+-------+     |      (Docker)         |
|      Hypervisor       |     +-----------------------+
+-----------------------+     |   Host operating      |
|  Host OS / hardware   |     |   system              |
+-----------------------+     +-----------------------+
                              |   Infrastructure      |
                              +-----------------------+

How a container works

A container:

  • uses the resources of the host system (its CPU, memory and operating system);
  • behaves like an isolated machine inside the host machine;
  • is isolated: it cannot access or interfere with the host, and is agnostic to it. You cannot mess up the host even if you try.

Container, as the course puts it

A container is like an isolated machine that uses the resources of the host machine. We build the container and ship it to an orchestrator, and it works everywhere the same.

See Containerization for the general concept and Docker for the tool.

From outside the course material (Docker documentation and Linux background), the parts the course leaves implicit:

  • An image is the package: a read-only file system snapshot (base OS files, Python, your libraries, your code) plus metadata such as the default command. A container is a running instance of an image, with a thin writable layer on top. Many containers can run from one image, and what a container writes is lost when it is removed, unless it goes to a mounted volume.
  • An image is built in layers, one per build instruction, and layers are cached: change one line of code and only the layers from that instruction on are rebuilt.
  • Isolation comes from two Linux kernel features. Namespaces limit what a process can see (its own process list, network, file system and hostname). Control groups (cgroups) limit how much it can use (CPU, memory). A container is an ordinary process on the host, fenced in by these.
  • Because containers share the host’s kernel, “the same everywhere” has limits. A Linux image needs a Linux kernel: Docker Desktop on macOS and Windows runs a small Linux VM underneath. And an image is built for one CPU architecture: on an Apple Silicon Mac you build arm64 images by default, so the NumPy problem above can still bite unless you build for the server’s platform (--platform linux/amd64).
  • Isolation is the default, and you can open it on purpose: a mounted host folder, a published port or --privileged gives the container access to the host. Sharing a kernel also makes the isolation weaker than a VM’s.

Docker in practice

The lecture’s live coding built and ran containers from the course repository, whose code was not available for this note. What follows is from outside the course material (Docker documentation), written to fit the repository layout from week 1, where the Dockerfile lives in infra/.

A Dockerfile is the recipe for an image, read from top to bottom:

# base image: Debian plus Python 3.13
FROM python:3.13-slim
# all later paths are relative to /app
WORKDIR /app
# dependencies first: this layer stays cached
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# code last: it changes most often
COPY src/ src/
COPY models/ models/
# print output straight away (the flush problem)
ENV PYTHONUNBUFFERED=1
# default command when a container starts
CMD ["python", "src/main_training.py"]

Why this order: each instruction is a layer, and a changed layer invalidates every layer after it. With requirements.txt copied and installed before the code, editing src/ reuses the cached install, and rebuilds take seconds. Copy everything first and every code edit reinstalls every package.

Pin the base image tag. The demo’s python:3-slim resolved to Python 3.13.5 on that day and will resolve to something newer next year, which is the “works on my machine” problem coming back through the door. A .dockerignore file keeps data/, .venv/ and .git/ out of the build context, so they are not sent to the builder or copied into the image.

Building and running:

docker build -t fraud-model:0.1 -f infra/Dockerfile .   # "." = build context (repo root)
docker run --rm fraud-model:0.1                         # run the default command
docker run --rm -p 8000:8000 fraud-model:0.1            # publish container port 8000 on the host
docker run --rm -v "$(pwd)/data:/app/data" fraud-model:0.1   # mount host data into the container
docker run --rm -e MLFLOW_TRACKING_URI=http://host.docker.internal:5000 fraud-model:0.1

Debugging a container that misbehaves:

docker ps -a                       # list containers, including ones that exited
docker logs <container>            # its stdout and stderr: usually the traceback
docker exec -it <container> bash   # open a shell inside a running container
docker run -it --entrypoint /bin/bash fraud-model:0.1   # start a shell instead of the app
docker image inspect fraud-model:0.1   # default command, env vars, working directory

The --entrypoint /bin/bash run is the trick from the hello world demo: land in a shell inside the image and check by hand whether the files, versions and paths are what you expect. Common causes of a broken container:

  • It exits at once. A container lives exactly as long as its main process. If the script finishes or crashes, the container stops; docker logs shows why.
  • File not found. The file was never copied in (missing COPY, or excluded by .dockerignore), or a relative path assumes a different working directory than WORKDIR.
  • The API runs but cannot be reached. It listens on 127.0.0.1, which inside a container is the container itself. It must listen on 0.0.0.0, with the port published by -p.
  • localhost points at the wrong place. For the same reason, reach another container by its service name (Docker Compose, below) and the host by host.docker.internal on Docker Desktop.
  • No output appears. Python buffers output when it is not writing to a terminal; set PYTHONUNBUFFERED=1 or use flush=True.

MLflow: tracking experiments and models

MLflow is a tool for three jobs:

  • Experiment tracking: record each training run with its parameters, metrics and output files, so you can compare runs and reproduce the best one.
  • Model registry and management: keep versions of trained models in one place.
  • Model serving: expose a registered model behind an API.

It is popular, and in the lecturer’s words definitely optional for more than 90% of projects. The course uses it to log three things per run: the model (the model file as a pickle), an artifact (any file) and a metric (the F1 score). In the demo, a run of experiment “Alpha” shows the metrics test_f1_mean (0.68) and test_f1_std, and two artifacts: alpha.pkl, the pickled model, and cv_scores.json, the per-fold fit and score times and train and test precision, recall and F1.

See MLflow for more. The demo’s code was not available. Below, from outside the course material (MLflow documentation), is one way to log the same three things. The cv_scores.json keys match what scikit-learn’s cross_validate returns with training scores on and precision, recall and F1 scoring, so the demo probably used it (an inference).

import json, pickle
import mlflow
from sklearn.model_selection import cross_validate
 
mlflow.set_tracking_uri("http://localhost:5000")   # the MLflow server
mlflow.set_experiment("Alpha")
 
with mlflow.start_run():
    scores = cross_validate(model, X, y, cv=5, return_train_score=True,
                            scoring=["precision", "recall", "f1"])
    mlflow.log_metric("test_f1_mean", scores["test_f1"].mean())
    mlflow.log_metric("test_f1_std", scores["test_f1"].std())
 
    with open("cv_scores.json", "w") as f:
        json.dump({k: str(v) for k, v in scores.items()}, f)
    mlflow.log_artifact("cv_scores.json")             # any file
 
    model.fit(X, y)
    with open("alpha.pkl", "wb") as f:
        pickle.dump(model, f)
    mlflow.log_artifact("alpha.pkl")                  # the model as a pickle

The MLflow server is itself just another service, which you can start in its own container and point the training container at through MLFLOW_TRACKING_URI. That makes it one of the external systems in the next section.

Your application is never alone

Unless you are building a demo, your application is never the only software. A Python ML application typically sends SQL queries to a database, logs runs to an MLflow tracking server and uploads files to cloud storage. These systems will not run on your machine in production, so you have to build, and test, with external dependencies.

Unit Testing covers the units of your own code, with external dependencies mocked. But how can you be sure that the SQL you wrote is actually correct? Take SELECT * FR0M table WHERE a>b, with a zero in place of the O in FROM. A mock returns whatever rows you tell it to, so that query passes every unit test, and so do a wrong column name and a type mismatch with the real table.

The testing pyramid

Three levels of tests

  • Unit tests test your code.
  • Integration tests test your code’s integration and interaction with other systems.
  • End-to-end (E2E) tests simulate the user’s interaction, taking the system as a whole from input to output.

All of them must be automated.

            /\
           /  \        UI / end-to-end tests   (few)
          /----\
         /      \      integration tests
        /--------\
       /          \    unit tests              (many)
      /------------\

Moving up the pyramid, tests become more difficult to implement and it becomes more difficult to find the root cause of a failure: a failing unit test points at one function, a failing end-to-end test only says that something somewhere is wrong. So you write many unit tests, fewer integration tests (for example, the function that queries the database, run against a database) and a few end-to-end tests. Yesilbek’s own view: with AI-assisted engineering, testing, especially UI and end-to-end tests, is much more important now.

See Integration Testing.

Automating integration tests: more containers

The FIRST rules from week 1 say tests must be isolated and repeatable. An integration test, though, needs a real database. A shared development database fails both rules: other people change its data, and from a test machine you often cannot guarantee access to the tools in the test or development environments, or connect to them at all.

The answer is more containers. Put each external dependency in its own container and set it up exactly as the test expects. Containers are the modern way of deployment, so an image very likely exists already for each dependency, external (PostgreSQL, Redis, MLflow) or internal to your company. Docker Compose offers light orchestration of containers: one docker-compose.yml file describes several services (a web app, a database, a cache), and docker compose up starts them all on a shared network.

From outside the course material (Docker Compose documentation), an integration test setup with a PostgreSQL database, the compose file sitting in infra/:

services:
  db:
    image: postgres:16
    environment:
      POSTGRES_USER: test
      POSTGRES_PASSWORD: test        # throwaway test credentials only
      POSTGRES_DB: trx
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U test -d trx"]
      interval: 2s
      retries: 15
  tests:
    build:
      context: ..                    # repo root, relative to infra/
      dockerfile: infra/Dockerfile
    volumes:
      - ..:/app                      # mount the repo, so tests/ is inside
    environment:
      DATABASE_URL: postgresql://test:test@db:5432/trx   # "db" = the service name
    depends_on:
      db:
        condition: service_healthy   # wait until Postgres accepts connections
    command: ["python", "-m", "pytest", "tests/integration"]
# src/data_processing/queries.py
import pandas as pd
from sqlalchemy import text
 
def fetch_large_transactions(engine, min_amount):
    query = text("SELECT id, amount FROM trx WHERE amount > :m")
    return pd.read_sql(query, engine, params={"m": min_amount})
 
# tests/integration/test_queries.py
import os
import pytest
from sqlalchemy import create_engine, text
from src.data_processing.queries import fetch_large_transactions
 
@pytest.fixture
def engine():                                   # setup: a known table
    eng = create_engine(os.environ["DATABASE_URL"])
    with eng.begin() as conn:
        conn.execute(text("DROP TABLE IF EXISTS trx"))
        conn.execute(text("CREATE TABLE trx (id INT, amount FLOAT, fraud INT)"))
        conn.execute(text("INSERT INTO trx VALUES (1, 50, 0), (2, 150, 1), (3, 300, 0)"))
    yield eng
    eng.dispose()
 
def test_fetch_large_transactions(engine):      # collect and compare
    result = fetch_large_transactions(engine, min_amount=100)
    assert sorted(result["id"]) == [2, 3]
docker compose -f infra/docker-compose.yml up --build \
    --abort-on-container-exit --exit-code-from tests
docker compose -f infra/docker-compose.yml down -v      # remove containers and data

The details that usually go wrong:

  • Service names are host names. Inside the compose network the database is db:5432; localhost in the tests container is the tests container itself.
  • Started is not ready. Plain depends_on only waits for the database container to start, and Postgres needs a few seconds more to accept connections. The health check plus condition: service_healthy waits for readiness.
  • The test container needs the tests. The Dockerfile above copies only src/ and models/, so the compose file mounts the repository into /app. pytest and the Postgres driver (psycopg2-binary) must be in requirements.txt, and python -m pytest puts /app on the import path so from src... works.
  • Clean state per run. The fixture creates its own table, and down -v deletes the database volume, so every run starts from the same state: the test is repeatable.
  • --exit-code-from tests makes the command’s exit code the test result, so a CI pipeline (an automated build and test server) fails when the tests fail.

Mistakes happen, just not in production

In June 2021 some HBO Max subscribers received a mysterious email with the subject line “Integration Test Email #1”. Hours later the company said it was intended to be an empty test email, and “yes, it was the intern.”

Whose fault is this: the intern, the manager, HR, or the engineering processes? The lecturer’s answer is the engineering processes. Mistakes happen. They just cannot be allowed to happen in live environments. A process in which a test email can reach real subscribers is broken, whoever pressed the button.

Dev, Acc, Prod: layered environments

Week 1 introduced the layered environments: pressing a button that makes changes live is a bad idea, each environment runs the same tech stack, and only production is live and protected. This lecture adds what each one is for. See Development, Acceptance and Production Environments.

Local (your laptop). Unrestricted and personal: the easiest place to develop and debug.

Development (Dev), also called Test. A disorganised free-for-all, used mostly to develop against the tech stack and to experiment. It has the same tools you would have in production, or maybe more, but access to just your own systems. It is sandbox-like: do not expect guarantees, and things there are temporary.

Acceptance (Acc), also called Staging. A replica of production: the same tech stack, with all systems deployed, active and integrated with each other, so you can actually test the whole product. The only differences from production are scale and data: low scale, and no production data.

Production (Prod). The live systems, built at live scale, and the only environment that talks to users. The danger zone.

How code moves through them:

flowchart TD
    L["Laptop"] --> DEV["Dev: same tech stack, your systems only"]
    L --> CD(("CD"))
    CD --> ACC["Acc: same tech stack, all systems integrated"]
    CD --> PROD["Prod: live-scale tech stack, all systems integrated"]
    PROD <--> U["Users"]

You work directly against Dev from your laptop, but code reaches Acc and Prod only through continuous deployment (CD), an automated pipeline that deploys merged code (GitFlow, below, says which merge goes where).

Read the HBO Max case against this (the lecturer did not draw this link; it follows from the setup): an acceptance environment holds no production data, so a test email sent there has no real subscribers to reach.

GitFlow: branches that mirror environments

GitFlow organises a team’s work with git so that branches line up with the environments. It has two long-living branches:

  • main points to Prod;
  • develop points to Acc.

So the state of main is the state of production, and the state of develop is the state of acceptance. Feature branches introduce new features, refactoring, bug fixes and the like. Merging into main and develop is protected, and a merge triggers CD against the target environment: merge into develop and the change is deployed to acceptance; merge into main and it goes live.

gitGraph
    commit tag: "v0.1"
    branch develop
    commit
    branch feature
    commit
    commit
    checkout main
    branch hotfix
    commit
    checkout main
    merge hotfix tag: "v0.2"
    checkout develop
    merge hotfix
    merge feature
    branch release
    commit
    checkout main
    merge release tag: "v1.0"
    checkout develop
    merge release

The full model, from the Atlassian reading: feature branches start from develop and merge back into develop, and never touch main. A release branch starts from develop when enough features are in, takes only fixes and release tasks, then merges into main (tagged with a version number, v1.0 above) and back into develop. A hotfix branch is the only one that starts from main: it quickly patches production (v0.2) and merges into both main and develop, so the fix is not lost in the next release.

The Atlassian reading also calls GitFlow a legacy workflow, which has lost popularity to trunk-based development (everyone merges small changes into one main branch often) and can be awkward to use with CI/CD. The course uses it for the mapping of branches onto protected environments.

See GitFlow.

Pull requests: review before merge

Protected branches need a way in. A pull request (PR) is a request to the team to merge your changes: it opens the code changes for review. Team members must review and choose to approve or reject, and a PR cannot be merged if it is not approved.

  • Author and reviewers share the responsibility for what gets merged.
  • Seniority does not play a role: anyone can review anyone’s code.
  • It is a great way to increase quality and to learn from each other.
  • For some, ego may be at play: a reviewer may approach aggressively, and an author may take comments personally.

The reading (Blockly’s contributor guide on good PRs) adds the author’s side: tell the team what you will work on before you start; keep changes small and focused and fix one problem per PR; split a large change into logical commits; include unit tests for your changes; write a concise and complete description, by hand, so you know you understand what you submit; and treat the review as collaboration, pushing new commits to the same branch until it is approved.

Technical debt

Technical debt (Atlassian's wording, used in the lecture)

Technical debt is a metaphorical concept in software development that refers to the future cost incurred by choosing an easy or quick solution now instead of a better, more time-consuming approach.

The cartoon version: a builder asks why it takes so long to add a new window to a house held together by props and an umbrella. Every earlier shortcut makes the next change slower.

Like financial debt:

  1. Do not take the loan if you don’t have to. Do not introduce immediate tech debt.
  2. If you must take the loan, make it explicit and recorded.
  3. Do not go too much into debt.

This is the short version of Kästner’s chapter (week 1 reading), which adds Fowler’s quadrants (debt is deliberate or inadvertent, prudent or reckless, and ideally all of it is deliberate and prudent), the ML-specific kinds of debt and how to track repayment: see EPRM-Book Ch22 - Technical Debt. In the course project, a hard-coded path or a skipped integration test is a loan; a line in the README or an issue saying so is the record.

What to be able to do

  • Explain the “works on my machine” problem with the hello world and the NumPy examples, and the four levels where machines differ.
  • Contrast bare servers, virtual machines and containers, and explain why containers are lighter.
  • Explain image, container and layer, write a Dockerfile with a sensible layer order, build and run it, and debug a broken container.
  • Say what MLflow tracks, log a metric, an artifact and a model, and say when you do not need it.
  • Place unit, integration and end-to-end tests on the pyramid and explain the trade-offs.
  • Run an automated integration test against a database with Docker Compose, and explain service names, health checks and clean state.
  • Describe Local, Dev, Acc and Prod, and say what separates acceptance from production.
  • Map GitFlow’s main and develop onto environments, and explain feature, release and hotfix branches, pull requests and CD.
  • Apply the three technical debt rules to your own project.

Exam questions

Flashcards