Skip to main content
Welcome — in this lesson you’ll build a Kubeflow Pipeline (KFP) component that trains a simple classifier on the classic Iris dataset, run the component locally (each component executes in its own container), and learn how to customize the component image by installing additional Python packages. This guide covers:
  • A minimal KFP component that trains a DecisionTree on the Iris dataset.
  • How to run the component locally using Docker.
  • The common failure when required packages are missing and how to fix it with packages_to_install.
  • A fully working script and expected runtime behavior.
Relevant links:

Dataset overview

  • The Iris dataset contains feature vectors (iris.data) and class labels (iris.target).
  • In this example we train a DecisionTreeClassifier and predict the class for a single sample.

Minimal KFP component (initial example)

This minimal component uses the python:3.10-slim base image. Many minimal Python images do not include scientific libraries such as scikit-learn, so this example may fail unless scikit-learn is available in the image.

Run the component locally using Docker

During development it’s convenient to run KFP components locally. Use kfp.local with DockerRunner() so each component runs in its own Docker container.

What happens when a required package is missing

If the base image doesn’t include scikit-learn, the component container will fail with a ModuleNotFoundError. Typical error lines:

Cause

The python:3.10-slim image is intentionally minimal and does not ship with many ML libraries.

Fix

The @dsl.component decorator accepts a packages_to_install argument. KFP will run pip install for the listed packages inside the component container at startup. Use packages_to_install to install scikit-learn (or any other required packages) before your component code executes.
Use packages_to_install when your component imports libraries that are not included in the base image. This runs pip install inside the component container at startup.

Corrected full script (working version)

Below is the complete working script that initializes the local Docker runner and installs scikit-learn inside the component container before execution.

Expected runtime behavior

When you run the corrected script:
  1. KFP pulls the python:3.10-slim base image (if not already present).
  2. KFP installs scikit-learn inside the container (via pip).
  3. The component code runs and prints the prediction.
  4. The component returns the result as an output parameter.
Example summarized output:
Local output parameter (JSON):

Quick reference

Summary

  • Define components with @dsl.component(base_image=...).
  • If your component imports packages not present in the base image, add them to packages_to_install=[...].
  • Use kfp.local plus DockerRunner() to run and test components locally before deploying to a cluster.

Watch Video