Sub topic 9
Packaging the service in a container
This chapter assumes you have the main.py service from the earlier chapters, a trained model.joblib, and a working Docker installation. You should also be comfortable running commands in a terminal.
Why package the service
The service works on your machine because your Python version, library versions, and model file are all in place. A container image bundles these together with the code. The same image then runs the same way on a laptop, a test server, or a cloud host.
The model file is part of the image here. That keeps the example simple and makes each image self-contained. The image must be rebuilt when the model changes.
Pin the dependencies
The Dockerfile installs packages from requirements.txt. The model was pickled by joblib, and scikit-learn can fail to load a model that was saved with a different version. Pin the version you trained with.
fastapi==0.115.0
uvicorn==0.30.6
scikit-learn==1.5.2
joblib==1.4.2
pydantic==2.9.2Run pip freeze in your training environment and copy the exact scikit-learn version. The numbers above are examples.
The Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY main.py model.joblib ./
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]Each instruction does one job:
FROM python:3.11-slimstarts from a small Python image.WORKDIR /appsets the folder for the later commands.COPY requirements.txt .andRUN pip installinstall dependencies. They come before the app code on purpose.COPY main.py model.joblib ./adds the code and the model.EXPOSE 8000documents the port. It does not publish it.CMDstarts Uvicorn.
Layer caching
Docker builds an image in layers and reuses a layer if its inputs have not changed. Code changes often, and dependencies rarely do. Installing dependencies first means an edit to main.py only rebuilds the last layers. The slow pip install step is skipped.
Build and run
docker build -t iris-api .
docker run --rm -p 8000:8000 iris-apiIn another terminal, check the service:
curl http://localhost:8000/health
curl -X POST http://localhost:8000/predict \
-H "Content-Type: application/json" \
-d '{"sepal_length": 5.1, "sepal_width": 3.5, "petal_length": 1.4, "petal_width": 0.2}'The -p 8000:8000 flag maps port 8000 on your machine to port 8000 in the container. The health response should show "model_loaded": true. That confirms the model file made it into the image and loaded at startup.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `curl` cannot connect | Port not published | Add `-p 8000:8000` to `docker run` |
| Still cannot connect | Uvicorn bound to `127.0.0.1` | Use `--host 0.0.0.0` in `CMD` |
| `FileNotFoundError` for `model.joblib` | File missing from the build context | Run `docker build` in the folder that holds the file |
| Warning or error when loading the model | scikit-learn version differs from training | Pin the training version |
Next steps
The health endpoint can now serve as a container health check. Orchestrators can call it to decide whether to send traffic to a replica. Later chapters on scaling build on this.
Exercise
You change one line in main.py and rebuild. Why does the pip install step not run again, and what would happen if the Dockerfile copied all files before installing dependencies?
Show answer
Docker reuses a layer when its inputs are unchanged. The pip install layer depends only on requirements.txt, which did not change. If all files were copied first, the edit to main.py would change that earlier layer. Docker would then rebuild every later layer, including the slow pip install.
Exercise
The container starts and logs show Uvicorn running, but curl http://localhost:8000/health from your machine is refused. Name two causes and the fix for each.
Show answer
Either the port was not published, so add -p 8000:8000 to docker run, or Uvicorn is bound to 127.0.0.1 inside the container, so start it with --host 0.0.0.0.