Content ITV PRO
This is Itvedant Content department
Learning Outcome
4
Apply exact pins and lock files for reproducible builds.
3
Recognize how instruction order affects layer caching.
2
Identify the core instructions: FROM, WORKDIR, COPY, RUN, CMD.
1
Understand how a Dockerfile builds an image.
5
Connect version pinning to stable GenAI apps.
What is a Dockerfile ?
A plain text file, named exactly Dockerfile, listing the instructions to build an image.
Docker build reads it and produces a named, tagged image.
Each instruction creates a layer in the image.
Why it matters ?
Your own API image must come out identical on your laptop today and on a build server months later.
Code
docker build -t churn-api:1.0 .Core Dockerfile Instructions
FROM
WORKDIR
COPY
RUN
The base image to start from. Every Dockerfile begins here.
The folder inside the image where later steps run.
Copy files from your project into the image.
Run a command at build time, e.g., pip install. Each RUN adds a layer.
ENV
EXPOSE
CMD
Set an environment variable, e.g., PYTHONUNBUFFERED=1 for instant logs.
Documents the port; it doesn't publish it (you still need -p).
The default command when the container starts.
Layer Caching and Instruction Order (Core Mechanism)
Step 5
Step 4
Step 3
Step 2
Step 1
FROM and WORKDIR set the stable base.
COPY requirements.txt alone.
RUN pip install. This layer is reused until dependencies change.
COPY . . brings in the code last, so code edits rebuild only this small layer.
CMD starts the API on 0.0.0.0.
Putting COPY . . before pip install turns a 5-second rebuild into a 5-minute one. A .dockerignore keeps .git, venv, datasets, and .env out of the image.
Code
FROM python:3.11.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]requirements.txt
POST /predict/batch
Uvicorn (an ASGI server) runs the app.
4
Create the app
The app object holds every endpoint.
1
Define a path operation
A decorator links a method + URL to a function.
2
POST /predict
Return a dict; FastAPI sends it as JSON with status 200.
3
Layer Caching and Instruction Order (Core Mechanism)
POST /predict/batch
Uvicorn (an ASGI server) runs the app.
4
Create the app
The app object holds every endpoint.
1
Define a path operation
A decorator links a method + URL to a function.
2
POST /predict
Return a dict; FastAPI sends it as JSON with status 200.
3
What is FastAPI?
A modern, open-source Python framework for web APIs (Sebastián RamÃrez, 2018), the most popular choice for model serving.
Type hints drive everything
Model_name: str or limit: int tell FastAPI how to read, convert, and validate input.
Automatic docs
Live, testable documentation at /docs (Swagger UI) and /redoc.
High performance
Runs on ASGI, supporting async code.
Built on Starlette (web layer) and Pydantic (validation).
GET Endpoints and Parameters
Path parameters
Query parameters
Part of the URL, in curly braces; identify a specific resource.
Any argument not in the path; for optional filters, sorting, paging, with defaults.
Code
@app.get("/models/{model_name}")
def get_model(model_name: str): ...
@app.get("/predictions")
def list_predictions(limit: int = 10): ...Auto conversion
?limit=50 arrives as text and becomes the int 50. ?limit=abc is rejected before your function runs.
Loading the Model Once at Startup
Describe the input with a class inheriting Pydantic's BaseModel.
Accept it as a function argument; FastAPI reads and checks the JSON body.
Use the data as data.tenure_months, already the right type.
Return the result as a dict; it goes back as JSON.
Code
class ChurnInput(BaseModel):
tenure_months: int
monthly_fee: float
support_tickets: int
@app.post("/predict")
def predict(data: ChurnInput): ...Loading the Model Once at Startup
Loading a model takes seconds, so never load inside an endpoint.
Lifespan function: code before yield runs once at startup; code after yield runs once at shutdown.
Every request reuses the model already in memory.
@app.on_event("startup") in older tutorials is deprecated; use lifespan.
Good habit /health should report whether the model actually loaded.
Code
@asynccontextmanager
async def lifespan(app):
models["churn"] = mlflow.pyfunc.load_model("models:/churn-model@champion")
yield
app = FastAPI(lifespan=lifespan)def vs async def
Async def for waiting on other services
e.g., calling an LLM provider with an async client; the server handles other requests during the wait.
Mistake to avoid
slow blocking code inside async def freezes the whole server, even /health.
Def for model predictions
model.predict() is CPU-heavy and blocking; FastAPI runs a plain def in a worker thread, so other requests keep flowing.
GET Endpoint vs POST Endpoint (Comparison)
| Aspect | Unpinned | Pinned |
|---|---|---|
| What installs | Newest on build day | Exactly the listed versions |
| Same build next month | May differ | Identical |
| Model compatibility | Can silently break a saved model | Matches the training environment |
| Updating | Happens by accident | Happens deliberately, with tests |
| Typical use | Throwaway notebooks | Deployed APIs, CI builds, shared projects |
| Analogy | "Light blue, nicely" | Reservation counter |
Think About This: Spotting the Difference
FROM python:latest
Not reproducible: latest moves to new Python versions over time.
Not reproducible: both resolve to newer versions on every build.
scikit-learn>=1.3 and fastapi
Reproducible: The digest identifies one exact image.
Pin everything that could change underneath you.
FROM python:3.11.9-slim@sha256:…
Pin everything that could change underneath you.
Real-World Applications
One image everywhere
Laptop, CI, and cloud run the same pinned build.
Large models
Downloaded at startup from the MLflow Registry instead of baked into huge images.
Safe upgrades
Dependabot opens a version-bump PR; tests decide if it ships.
GenAI apps
Pin LangChain, LLM SDKs, and model names; they change interfaces often.
Summary
5
Pin GenAI SDKs; they change fast.
4
Pin the base image tag or digest.
3
Pin with ==; lock transitive packages.
2
Stable steps first, code last.
1
Dockerfile: step-by-step image build instructions.
Quiz
Every small edit to main.py triggers a 5-minute pip reinstall during docker build. What is the most likely cause?
A.The base image is python:3.11-slim
B.The Dockerfile has no EXPOSE line
C.COPY . . comes before RUN pip install
D.requirements.txt uses exact pins
Quiz-Answer
Every small edit to main.py triggers a 5-minute pip reinstall during docker build. What is the most likely cause?
A.The base image is python:3.11-slim
B.The Dockerfile has no EXPOSE line
C.COPY . . comes before RUN pip install
D.requirements.txt uses exact pins
By Content ITV