Skip to main content

Python API in a container

Published

2026-12-02

In Lab 6, we installed Docker Desktop and moved the Python and R APIs into _labs/lab06/. This chapter puts the Python vetiver API from Lab 3 in a Docker container. We’ll run the API locally first, then build the image and run it with the model mounted from the host.

Set up

Review the original lab 6 files here. I’ve created both Python and R versions, and you can access and run the API from the Python/api/ folder.

First, check the Python version:

which python3
/usr/bin/python3
python3 --version
Python 3.12.3

Create a virtual environment using venv:

/usr/bin/python3 -m venv .venv 
source .venv/bin/activate  

Install libraries in the requirements.txt:

pip install -r requirements.txt

Build model (optional)

We don’t have to, but we can rebuild the model with model.py. It also needs palmerpenguins and duckdb, and requirements.txt already installed both.

Run the model.py script:

python3 model.py

This will create a new model in models/ (if something changed).

  species     island  bill_length_mm  bill_depth_mm  flipper_length_mm  body_mass_g     sex  year
0  Adelie  Torgersen            39.1           18.7              181.0       3750.0    male  2007
1  Adelie  Torgersen            39.5           17.4              186.0       3800.0  female  2007
2  Adelie  Torgersen            40.3           18.0              195.0       3250.0  female  2007
R^2 0.8555368759537614
Intercept 2169.2697209393996
prototype_data      bill_length_mm  species_Chinstrap  species_Gentoo  sex_male
0              39.1              False           False      True
1              39.5              False           False     False
2              40.3              False           False     False
4              36.7              False           False     False
5              39.3              False           False      True
..              ...                ...             ...       ...
339            55.8               True           False      True
340            43.5               True           False     False
341            49.6               True           False      True
342            50.8               True           False      True
343            50.2               True           False     False

[333 rows x 4 columns]
Columns Index(['bill_length_mm', 'species_Chinstrap', 'species_Gentoo', 'sex_male'], dtype='object')
Coefficients [  32.53688677 -298.76553447 1094.86739145  547.36692408]
Model Cards provide a framework for transparent, responsible reporting. 
 Use the vetiver `.qmd` Quarto template as a place to start, 
 with vetiver.model_card()
Writing pin:
Name: 'penguin_model'
Version: 20260618T104900Z-91fdd

palmerpenguins also prints a UserWarning that pkg_resources is deprecated; it’s harmless, and requirements.txt pins setuptools below 81 so the import still works. If the model hasn’t changed, vetiver skips the write and prints The hash of pin "penguin_model" has not changed. Your pin will not be stored. instead of Writing pin:.

Run the API

Add os.environ['PINS_ALLOW_PICKLE_READ'] to the top of mod-api.py, right after import os and before importing pins.

# UPDATED mod-api.py

import os
os.environ['PINS_ALLOW_PICKLE_READ'] = '1'

import warnings
warnings.filterwarnings("ignore", message=".*urllib3 v2 only supports OpenSSL.*")

import vetiver
import pins
import uvicorn
import pandas as pd


model_path = os.environ.get("MODEL_PATH", "models/")
model_board = pins.board_folder(model_path)

sklearn_model = model_board.pin_read("penguin_model")
print(f":-] Successfully loaded model: {type(sklearn_model)}")

if hasattr(sklearn_model, 'feature_names_in_'):
    feature_names = sklearn_model.feature_names_in_
    print(f"Model expects features: {list(feature_names)}")
    
    def get_prototype_value(column_name):
        """Get appropriate default value for each column type"""
        if 'bill_length' in column_name:
            return 45.0  
        elif 'species_Gentoo' in column_name:
            return 1     
        elif 'sex_male' in column_name:
            return 1     
        else:
            return 0
    
    prototype_data = pd.DataFrame({
        name: [get_prototype_value(name)] for name in feature_names
    })
    
else:
    
    print("Model doesn't have feature_names_in_, using estimated prototype")
    prototype_data = pd.DataFrame({
        "bill_length_mm": [45.0],
        "species_Chinstrap": [0],
        "species_Gentoo": [1], 
        "sex_male": [1]
    })

print("Prototype data shape:", prototype_data.shape)
print("Prototype columns:", list(prototype_data.columns))
print("Sample data:")
print(prototype_data)

v = vetiver.VetiverModel(
    model=sklearn_model, 
    model_name="penguin_model",
    prototype_data=prototype_data
)
print(f":-] Created VetiverModel: {type(v)}")

vetiver_api = vetiver.VetiverAPI(v, check_prototype=True)
# can run with:
# vetiver_api.run(port = 8080)

# actual FastAPI application
app = vetiver_api.app

print(f"FastAPI app type: {type(app)}")

if __name__ == "__main__":
    print(":-] Starting Penguin Model API...")
    print(":-] API Documentation: http://127.0.0.1:8080/docs")
    print(":-] Health Check: http://127.0.0.1:8080/ping")
    print(":-] Model Info: http://127.0.0.1:8080/metadata")
    uvicorn.run(app, host="0.0.0.0", port=8080)
1
Set environment variable BEFORE importing pins
2
Import packages
3
Load model from MODEL_PATH environment variable (default: local models/ for dev)
4
Read pinned model
5
Check model for the features it expects
6
Define prototype value logic (realistic penguin bill length, default to Gentoo species, default to male, default for other dummy variables)
7
Create prototype data directly
8
Fallback when model doesn’t have feature names
9
Wrap as VetiverModel
10
Create VetiverAPI and extract FastAPI app
11
Print API info and start the server on port 8080

Finally, run the API using:

python3 mod-api.py

The API prints the model details, then uvicorn reports the server address:

:-] Successfully loaded model: <class 'sklearn.linear_model._base.LinearRegression'>
Model expects features: ['bill_length_mm', 'species_Chinstrap', 'species_Gentoo', 'sex_male']
Prototype data shape: (1, 4)
Prototype columns: ['bill_length_mm', 'species_Chinstrap', 'species_Gentoo', 'sex_male']
Sample data:
   bill_length_mm  species_Chinstrap  species_Gentoo  sex_male
0            45.0                  0               1         1
:-] Created VetiverModel: <class 'vetiver.vetiver_model.VetiverModel'>
FastAPI app type: <class 'fastapi.applications.FastAPI'>
:-] Starting Penguin Model API...
:-] API Documentation: http://127.0.0.1:8080/docs
:-] Health Check: http://127.0.0.1:8080/ping
:-] Model Info: http://127.0.0.1:8080/metadata
INFO:     Started server process [82243]
INFO:     Waiting for application startup.
INFO:     VetiverAPI starting...
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)

View the API using the following URLs:

http://127.0.0.1:8080/

http://127.0.0.1:8080/docs

Testing API (optional)

We can perform some terminal testing, too (in a new terminal).

Test health check:

curl http://127.0.0.1:8080/ping
{"ping":"pong"}

Test prediction using the following:

curl -X POST "http://localhost:8080/predict" \
  -H "Content-Type: application/json" \
  -d '[{"bill_length_mm": 45.0, "species_Chinstrap": 0, "species_Gentoo": 1, "sex_male": 1}]'

The response holds the predicted body mass in grams:

{"predict":[5275.663941169738]}

vetiver typically expects data as a list of observation records. The outer [...] wraps a single record so it becomes one row in the DataFrame.

Make sure to quit (Ctrl + c) the API before building the Docker image and running the model in the container.

Docker

Create a Dockerfile in the _labs/lab06/Python/api/ project root folder:

├── api.Rproj
├── Dockerfile
├── mod-api.py
├── model.py
├── models
├── my-db.duckdb
├── README.md
├── requirements-api.txt
└── requirements.txt

2 directories, 8 files
1
The new Dockerfile

Enter the following:

FROM python:3.11-slim

WORKDIR /app

COPY requirements-api.txt .
RUN pip install --no-cache-dir -r requirements-api.txt

COPY mod-api.py .
COPY models models/

ENV MODEL_PATH=/app/models
ENV PINS_ALLOW_PICKLE_READ=1

EXPOSE 8080

CMD ["python3", "mod-api.py"]
1
official Python 3.11 slim image as the base
2
working directory inside the container to /app
3
copies the API requirements file from the host into the container
4
installs Python dependencies
5
copies the FastAPI application script into the container
6
copies the pinned model board (models/) into the image at /app/models
7
sets an environment variable pointing to the model board
8
lets pins read pickle (joblib) model files
9
exposes port 8080 so the API can be accessed externally
10
runs the FastAPI application when the container starts

The python:3.11-slim base image minimizes container size. It runs Python 3.11 rather than the local 3.12.3. The mismatch is safe because requirements-api.txt pins the same scikit-learn version (1.6.1) used to train the model.

The --no-cache-dir flag stops pip from caching downloaded packages, which keeps the image smaller. The ENV lines are environment variables (the path to the model board and the pickle setting). EXPOSE only documents that the API listens on port 8080 (the port set in mod-api.py); the -p flag in docker run publishes it.

Start Docker Desktop:

systemctl --user start docker-desktop

Build the image

Build the image (run from Python/api/):

docker build -t penguin-model-py .

In the Positron/RStudio Terminal, you should see something like:

When the image is built, we can see it under Builds in Docker Desktop:

Run the container

Run the container, mounting the model directory from the host:

docker run --rm -d \
  -p 8080:8080 \
  --name penguin-model-py \
  -v "$(pwd)/models":/app/models \
  penguin-model-py

The image already holds a copy of models/, so the container runs without the -v flag. The -v flag mounts the local models/ folder over that copy at /app/models, where MODEL_PATH points. A new model version on the host then reaches the API without a rebuild.

In Docker Desktop, click on the Containers tab and you’ll see the container running:

Test the running container:

curl http://localhost:8080/ping

The response should be:

{"ping":"pong"}

Stop the container

To stop the container, run the following:

docker stop penguin-model-py

Since we used --rm in the run command, the container will be automatically removed when it stops.

If the graceful stop doesn’t work, you can force kill it:

docker kill penguin-model-py

Final project files

The finished api/ folder now looks like this:

├── api.Rproj
├── Dockerfile
├── mod-api.py
├── model.py
├── models/
├── my-db.duckdb
├── README.md
├── requirements-api.txt
└── requirements.txt

5 directories, 12 files
1
Container definition to build and run the API in Docker.
2
FastAPI application that loads the ML model and exposes REST endpoints (ping, metadata, predict).
3
Script that trains a linear regression model on Palmer penguin data and pins it with vetiver and pins.
4
Directory containing timestamped model versions with .joblib model files and data.txt metadata.
5
DuckDB database file storing the Palmer penguins dataset.
6
Setup and usage instructions for running the API locally and in Docker.
7
Python dependencies for the FastAPI application.
8
Python dependencies for both model training and the API.

Recap

  1. The Dockerfile starts from python:3.11-slim, installs only the API dependencies from requirements-api.txt, copies mod-api.py and models/, and sets MODEL_PATH and PINS_ALLOW_PICKLE_READ as environment variables. The same pickle setting sits at the top of mod-api.py, before pins is imported, so the API can read the pinned model.

  2. The image carries a copy of the model board. Running the container with -v "$(pwd)/models":/app/models mounts the local models/ folder over it, where MODEL_PATH points, so a new model version doesn’t require a rebuild.

  3. A {"ping":"pong"} response from curl http://localhost:8080/ping confirms the API is running inside the container. Because we started it with --rm, docker stop also removes the container.

In the next chapter, we’ll run the R API in a Docker container.