Phase 6: Docker
Delivered in Pull Request #7
Pull request #7 (phase-six) merged the Docker train and serve images into main. Its Files changed tab shows everything this phase delivered.
Status: Built and checked. Both images build and run on this computer, and the containers reproduce the project's numbers. The Implementation section below records what was checked, and the Development Guide section below shows how to build and run them.
Needs first: Phases 1 to 5, which are built, and Docker Desktop, running.
Phase 6 packages the project so it runs from a container. One Dockerfile has two targets. The train target trains the model from the committed data and logs the run to MLflow. The serve target starts the same FastAPI service as Phase 3, with a model trained while the image is built. A person with Docker and a copy of the repository can train and serve without installing Python or any package. The container answers the worked example with the same probability as the local service, 0.3296.
The images run on a laptop only. AWS keeps running the API from the Phase 4 zip, and the web app is unchanged.
How Do the Pieces Fit Together?
flowchart TB
R["requirements.txt<br/>(exact pins)"] --> B
S["src/, config/, tests/,<br/>data/ (committed)"] --> B
B["Base stage<br/>Python 3.13.14 slim,<br/>every package installed"]
B --> T["train target<br/>runs python -m src.model.train"]
B --> M["Build step<br/>trains once, keeps models/model.joblib"]
M --> V["serve target<br/>runs uvicorn on port 8000"]
V -->|"POST /predict<br/>GET /health"| C["curl on this computer<br/>http://127.0.0.1:8000"]
- One install, two entry points. Both targets start from the same base stage, so the Python version and every package pin are written once. This follows the choice in Design Manifest.
- The data is already in the repository. Training reads
data/from-s3/nfl_matchups_2020_2026.csv, which is committed, so training needs no AWS credentials and no download. Only the S3 upload and download steps need credentials. - The serve image carries its own model.
models/model.joblibis git-ignored, so a fresh copy of the repository has no model. Training during the build meansdocker build --target servealone gives a working service.
What Does Each Target Do?
The train Target
- Runs:
python -m src.model.train, the same command asmake train. - Writes, inside the container:
models/model.joblib,mlflow.db,mlartifacts/,results/run-summary.json, and the readable copy inmlflow-reference/. - Keeps only the run summary on this computer. Every other file is deleted with the container.
make docker-trainmountsbuild/docker/results/over the container'sresults/, so the newrun-summary.jsoncan be checked against the committed one. - Can also run the tests:
python -m pytestinside the same image, with no extra install. - Can also run the data steps: the image holds the whole
src/folder, so the Phase 1 data steps and the two web app data steps run in it too. See Run the Data Steps in the Container.
The serve Target
- Runs:
uvicorn src.api.app:appon port 8000 inside the container, listening on every address the container has, so Docker can forward a port to it. - Published only to this computer: the run command maps
127.0.0.1:8000to the container, so nobody else on the network can reach it, as in Phase 3. - Routes:
POST /predictandGET /health, the Phase 3 service unchanged. The feedback route stays on AWS only, because it writes to a DynamoDB table.
Steps in Order
- Step 1. Install Docker Desktop and check that
docker versionreports both a client and a server (Engine 29.9.3). - Step 2. Write
.dockerignore, which leaves everything out of the image unless it is named. - Step 3. Write the
Dockerfilewith a base stage and thetrainandservetargets. The image runs as a user that is not root, and it holds no AWS credentials. - Step 4. Build and run the
traintarget. The scores and the run summary's SHA-256 match the committed ones. - Step 5. Run the tests inside both images. The counts are under Results.
- Step 6. Build and run the
servetarget./healthand the three Phase 3 examples give the same answers as the local API. - Step 7. Check what is inside the image: no
.git/, no secrets, nomlflow.dbfrom this computer, and a user that is not root. - Step 8. Write it down: the Development Guide and Implementation sections below, and the Docker entries in the Requirements Rubric and Design Manifest.
Results
- Training in the container: validation 63.10% (171 of 271) and test 59.06% (88 of 149), and a run summary byte for byte the committed one.
- The service in the container: the worked example gives 0.3296, and the other two Phase 3 examples give 0.5364 and 0.6467.
- Tests: 93 passed and 2 skipped in the train image (it has no saved model), 95 passed in the serve image, and 95 passed on this computer.
- Images: about 1.4 GB each on disk. The first build takes about a minute, and a rebuild with no change takes under a second.
Decisions Made
- Docker Desktop for Mac, the most common choice. Colima is a free command-line alternative that runs the same
dockercommands. - Train the model during the serve build, so the serve image works from a fresh copy of the repository. Turned down: mounting
models/from this computer, which fails when no model has been trained yet and makes the image depend on files outside it. - Install
requirements.txtin both targets. The serve target then carriespandasandmlflow, which it does not use, but one install keeps the two targets on the same pins. Turned down:requirements-serve.txtfor the serve target, because it leaves outuvicornand would need a second list of pins. - Pin the base image to
python:3.13.14-slim, so a rebuild months later uses the same Python. Python 3.13 matches this computer and the Lambda function. - Build for this computer's chip only. This Mac uses
arm64. Building for other chips is not needed for a laptop image. - Accept that a container run records no git commit.
.git/is left out of the image, and the export still finishes without it. - Two make commands,
make docker-trainandmake docker-serve. See Makefile.
What Was Created
- Files:
Dockerfileand.dockerignorein the project folder, and two commands in theMakefile. - Images on this computer:
510-m3-nfl-trainand510-m3-nfl-serve. They are not pushed anywhere. - Docs: this page, including its Development Guide and Implementation sections.
- No AWS changes.
What Is Not in This Phase
- Pushing the image to a registry such as Amazon ECR, or running Lambda from an image. The zip fits Lambda's limit, as measured in What If the Zip Is Too Large? in Phase 4.
- The web app in a container. It is static files on CloudFront and needs no server.
- Building the image in GitHub Actions.
- The README and the final review, in Phase 7.
Where to Read More
- Project Requirements lists the Docker requirement: every dependency installed, entry points for training and serving, and an image that builds and runs locally.
- Phase 2 Development Guide explains what a training run writes.
- Phase 3 Development Guide explains the requests and the answers the serve target gives.
- Design Manifest explains why Docker comes in this phase, and why there is one Dockerfile.
Development Guide
This section shows how to build and run the two Docker images on your own computer. A copy of the repository is all you need, because Python and the packages come inside the image.
What Is Needed First?
- Docker Desktop, running. Check it with the command below. It must print both a
Clientand aServersection. If the server part is missing, start Docker Desktop and try again.
docker version
- A copy of the repository. No virtual environment, no trained model, and no AWS account are needed. Training reads the committed data.
- Port 8000 free, for the serve image.
make startand a plainmkdocs serveboth use it.
Terms such as image, container, bind mount, and published port are in the Glossary.
How Do I Train in a Container?
make docker-train
It runs three steps:
- Makes the folder
build/docker/results/on your computer, if it is missing, and lets any user write to it. The container runs as user 10001, and on Linux a mounted folder keeps this computer's permissions. - Builds the
trainimage as510-m3-nfl-train. The first build downloads the base image and installs every package, which takes about a minute. Later builds reuse the cache and take about a second, unlessrequirements.txtchanged. -
Runs one container from it. The container trains, prints the same eight scoring lines as
make train, logs one MLflow run inside itself, and stops. The container is then removed. -
Expected scores: validation 63.10% (171 of 271) and test 59.06% (88 of 149).
- Three warnings about
gitare expected. The image has no.git/folder, so MLflow cannot record the commit. - Only the run summary is kept. The container's
results/folder is yourbuild/docker/results/folder, so itsrun-summary.jsonstays after the container is gone. Everything else it wrote is deleted with the container. Yourmlflow.db,models/, andmlflow-reference/are not touched.
Check That It Matches the Project
Compare the container's run summary with the committed one. The two lines must show the same SHA-256, bd14becd53dc68dc59da97addb67102e3f2e5a9051b8805ba40537d4d4d56112:
shasum -a 256 build/docker/results/run-summary.json results/run-summary.json
Run the Tests in the Container
docker run --rm 510-m3-nfl-train python -m pytest
Expected: 93 passed, 2 skipped. The two skipped tests need a saved model, and the train image has none until a container trains.
Run the Data Steps in the Container
The train image also holds the Phase 1 data steps and the two web app data steps, so the whole project can run in Docker. Build the image first (make docker-train does this), then run each step in its own container. Each container starts from the committed files inside the image and is removed when it stops, so a file it writes is gone afterward unless a folder from your computer is mounted over it.
Download the nflverse games file. It prints the address, the time, rows: 7548, and saved: data/raw/games.csv:
docker run --rm 510-m3-nfl-train python -m src.data.fetch_raw
Clean the games into the model's table, with your build/docker/processed/ folder mounted so the result is kept. Make the folder first, and let the container's user write to it, as make docker-train does for its folder:
mkdir -p build/docker/processed
chmod a+rwx build/docker/processed
docker run --rm -v "$PWD/build/docker/processed:/app/data/processed" 510-m3-nfl-train python -m src.data.clean
It prints the row counts at each filter, ending at 1,759 games, and writes build/docker/processed/nfl_matchups_2020_2026.csv. Its SHA-256 must be 7219e8a8672b900af9facbb54d8a28f9f2a34df9ede3b8385a735768082517fb, the same as the committed table:
shasum -a 256 build/docker/processed/nfl_matchups_2020_2026.csv
Print a summary of the two model inputs for each season group (training 1,339 rows, validation 271, test 149):
docker run --rm 510-m3-nfl-train python -m src.model.features
Build the web app's two data files. They print teams: 32 and games: 272 (150 final or tie, 122 upcoming). The files stay inside the container, so these runs check that the steps work and leave the committed copies alone:
docker run --rm 510-m3-nfl-train python -m src.profiles
docker run --rm 510-m3-nfl-train python -m src.season
The S3 upload and download need AWS credentials, which the image does not hold. To run them, mount your own AWS settings folder, read-only:
docker run --rm -v ~/.aws:/home/app/.aws:ro 510-m3-nfl-train python -m src.data.upload_to_s3
How Do I Run the API in a Container?
make docker-serve
It builds the serve image as 510-m3-nfl-serve, which trains the model once during the build, then starts the API at http://127.0.0.1:8000. The terminal stays busy while it runs. Use a second terminal for the checks below.
What the Output Means
CACHEDon every build step: nothing changed since the last build, so Docker reuses the saved image, training step included, and the build takes under a second.Uvicorn running on http://0.0.0.0:8000: that address is inside the container. On your computer, Docker publishes the port only on127.0.0.1, so only this machine can reach it.- Requests from
172.17.0.1: Docker's own address for your computer, as seen from inside the container. Every request from your browser orcurlshows this address. GET / HTTP/1.1" 404 Not Found: the API has no page at/. Its addresses are/predict,/health, and the test page/docs. A browser also asks for/favicon.icoby itself, which gets a 404 too.
Check That It Answers
curl http://127.0.0.1:8000/health
{"status":"ok","model_version":"v1.0","scikit_learn_version":"1.9.1"}
The worked example:
curl -X POST http://127.0.0.1:8000/predict -H "Content-Type: application/json" -d '{"win_pct_differential": -0.5, "point_diff_per_game_differential": -18.0}'
{"prediction":0,"predicted_label":"away win","home_win_probability":0.3296,"model_version":"v1.0"}
The same answer as the local API. More requests, and what each field means, are in How Do I Ask for a Prediction?. FastAPI's test page is at http://127.0.0.1:8000/docs.
Stop It
Press Ctrl+C in the terminal where it runs. The log shows Shutting down, the container is removed, and port 8000 is free again.
Run the Tests in the Serve Image
docker run --rm 510-m3-nfl-serve python -m pytest
Expected: 95 passed, because this image holds a model.
How Do I Change the Image?
- The files:
Dockerfileand.dockerignorein the project folder. - Rebuilding: both make commands build the image before they run it, so a change shows up the next time either command runs. A plain
docker runuses the image as it was last built. - Adding a file to the image: add a line starting with
!to.dockerignore. Everything not listed is left out. - Changing a package: change its pin in
requirements.txt. The next build installs it again. The laptop's.venv/and the image then both follow the same file. - Changing the Python version: change
PYTHON_VERSIONat the top of theDockerfile, and keep it on the same minor version as the laptop and the Lambda function (3.13).
How Do I Remove the Images?
The images use about 1.4 GB each on disk. To remove them:
docker image rm 510-m3-nfl-train 510-m3-nfl-serve
The next make command builds them again.
Implementation
This section records what goes into the image, how the Dockerfile is laid out, and the numbers the containers reproduced. The checks ran on Docker Desktop, Engine 29.9.3, linux/arm64.
What Goes Into the Image?
The .dockerignore file lists what may enter the image, and everything else is left out. Listing what to include is safer than listing what to leave out, because a new private file is excluded by default.
- Included:
requirements.txtandpytest.iniconfig/,src/, andtests/data/: the raw nflverse file, the cleaned table, and the copy downloaded from S3. All three are committed, and training readsdata/from-s3/nfl_matchups_2020_2026.csv.results/: the committed run summaryfrontend/src/data/: the two committed JSON files thattests/test_profiles.pyandtests/test_season.pycompare against
- Left out by default:
.git/and.venv/- the rest of
frontend/ build/,site/,models/,mlflow.db, andmlartifacts/mlflow-reference/, so a container run never adds to the committed run historydocs/,local/,local-docs/,specs/,.cursor/, and.kiro/- any
.env,*.pem, or credentials file - every
__pycache__/folder,.pycfile, and.DS_Storefile
- Checked:
/appin the image holds onlyconfig,data,frontend,pytest.ini,requirements.txt,results,src, andtests, and a search for*.pem,.env, and__pycache__finds nothing.
How Is the Dockerfile Laid Out?
flowchart TB
P["python:3.13.14-slim"] --> B["base stage<br/>a user that is not root,<br/>requirements.txt installed,<br/>project files copied"]
B --> T["train target<br/>CMD: python -m src.model.train"]
B --> S["serve target<br/>RUN: python -m src.model.train (at build)<br/>CMD: uvicorn on 0.0.0.0:8000"]
- The base stage:
- Starts from the official
python:3.13.14-slimimage, the same Python as this computer. Its digest when built wassha256:9662417aace5ae7b8e2609cce472b72a8958e134ba372808abe9cc1a0c0125e6. - Sets
PYTHONDONTWRITEBYTECODE=1,PYTHONUNBUFFERED=1(so log lines appear at once),PIP_NO_CACHE_DIR=1,PIP_DISABLE_PIP_VERSION_CHECK=1, andMLFLOW_DISABLE_AGENT_HINT=1. - Creates a user named
appwith id 10001, and uses/appas the working folder, owned by that user. - Copies
requirements.txton its own and installs it before copying the code, so a code change reuses the slow install step from Docker's cache. - Copies the included files, owned by
app, and switches to that user.
- Starts from the official
- The
traintarget adds nothing to the base. Its command ispython -m src.model.train. Because the base holds all ofsrc/, the same image also runs the data steps and the tests. The commands are under Run the Data Steps in the Container. - The
servetarget runspython -m src.model.trainonce while the image is built, so the image holdsmodels/model.joblib. Its command ispython -m uvicorn src.api.app:app --host 0.0.0.0 --port 8000. Inside a container,0.0.0.0is needed so Docker can forward a port to the service. Docker then publishes that port only on127.0.0.1, as described under TheserveTarget. - Exec form (
CMD ["python", ...]) is used for both commands, so Python receives Ctrl+C and the stop signal directly.docker stopended the service in 0.5 seconds. Ctrl+C ended it in under a second with exit code 0, and--rmremoved the container. serveis the last stage, so a plaindocker buildwith no--targetgives the service.
What Happens to MLflow's Git Details?
.git/ is left out of the image, and the slim image has no git. MLflow prints three warnings that it cannot find git, records no mlflow.source.git.* tags, and the export still finishes, ending with saved: mlflow-reference/latest-run. That copy stays inside the container. The committed history on this computer is never touched by a container run.
Which Make Commands Run It?
Two commands were added to the Makefile: make docker-train and make docker-serve. What each one runs is in How Do I Train in a Container? and How Do I Run the API in a Container?. The full list of make commands is on Makefile.
What Was Checked?
- Training in the container:
- The eight scoring lines match
make trainon this computer: training 61.24% (820 of 1,339), validation 63.10% (171 of 271), test 59.06% (88 of 149), validation log loss 0.6451, and test log loss 0.6815, with the same always-home baselines. build/docker/results/run-summary.jsonis byte for byteresults/run-summary.json(SHA-256bd14becd53dc68dc59da97addb67102e3f2e5a9051b8805ba40537d4d4d56112). The summary rounds every number to 6 places, which is why it can match across platforms. The model file is not compared byte for byte, because the container runs Linux and this computer runs macOS. The service's answers are checked instead.- The export to the container's own
mlflow-reference/finishes without.git/.
- The eight scoring lines match
- Tests in the container:
- The
trainimage, with no saved model: 93 passed, 2 skipped. The skipped tests are the last test intests/test_api.pyand the last intests/test_lambda_handler.py, which needmodels/model.joblib. - The
serveimage, which holds a model: 95 passed.
- The
- The service in the container:
GET /healthreturns{"status":"ok","model_version":"v1.0","scikit_learn_version":"1.9.1"}.- The worked example (
-0.5,-18.0) returnshome_win_probability0.3296, an away win. - Both inputs at 0 return 0.5364, and
0.25with10.0returns 0.6467, both home wins. - A request missing a field returns status 422.
- Docker publishes
127.0.0.1:8000->8000/tcp, with no0.0.0.0binding on this computer.
- What is inside the image: the user id is 10001, not root, and there is no
.git/,.env,*.pem,mlflow.dbfrom this computer, ormlflow-reference/. - Sizes and times:
510-m3-nfl-trainis 1.42 GB on disk and510-m3-nfl-serveis 1.43 GB. Both are 317 MB of compressed content, and the base image is 203 MB.- The first
make docker-traintook 57.9 seconds, 32.7 of them installing packages. A second build with no change took 0.6 seconds, because every step came from the cache. - The serve build took 6.4 seconds after the train build, 5.7 of them training.
- On this computer:
make pytestgives 95 passed,make web-test62 passed (the count when Phase 6 was built; the project now has 65 app tests in 12 files, or 63 passed and 2 skipped without thebuild/expected files), andresults/run-summary.jsonandmodels/model.joblibkept their bytes.
What Is Left Out
- Pushing an image, AWS changes, and a container for the web app, as listed under What Is Not in This Phase.
- A Docker health check, and images for other chips. The image is for this laptop.
- A smaller serve image. It installs all of
requirements.txt, includingpandasandmlflow, which the service does not use, so both targets keep the same pins.