|
| 1 | +# ContainerBackend |
| 2 | + |
| 3 | +The unified container backend for Kubeflow Trainer that automatically detects and uses either Docker or Podman. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This backend provides a single, unified interface for container-based training execution, automatically detecting which container runtime is available on your system. |
| 8 | + |
| 9 | +The implementation uses the **adapter pattern** to abstract away differences between Docker and Podman APIs, providing clean separation between runtime detection logic and container operations. |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### Basic usage (auto-detection) |
| 14 | + |
| 15 | +```python |
| 16 | +from kubeflow.trainer import TrainerClient, ContainerBackendConfig |
| 17 | + |
| 18 | +# Auto-detects Docker or Podman |
| 19 | +config = ContainerBackendConfig() |
| 20 | +client = TrainerClient(backend_config=config) |
| 21 | +``` |
| 22 | + |
| 23 | +### Force specific runtime |
| 24 | + |
| 25 | +```python |
| 26 | +# Force Docker |
| 27 | +config = ContainerBackendConfig(runtime="docker") |
| 28 | +client = TrainerClient(backend_config=config) |
| 29 | + |
| 30 | +# Force Podman |
| 31 | +config = ContainerBackendConfig(runtime="podman") |
| 32 | +client = TrainerClient(backend_config=config) |
| 33 | +``` |
| 34 | + |
| 35 | +### Configuration options |
| 36 | + |
| 37 | +```python |
| 38 | +config = ContainerBackendConfig( |
| 39 | + # Optional: force specific runtime ("docker" or "podman") |
| 40 | + runtime=None, |
| 41 | + |
| 42 | + # Optional: explicit image override |
| 43 | + image="my-custom-image:latest", |
| 44 | + |
| 45 | + # Image pull policy: "IfNotPresent", "Always", or "Never" |
| 46 | + pull_policy="IfNotPresent", |
| 47 | + |
| 48 | + # Auto-remove containers and networks on job deletion |
| 49 | + auto_remove=True, |
| 50 | + |
| 51 | + # GPU support (varies by runtime) |
| 52 | + gpus=None, |
| 53 | + |
| 54 | + # Environment variables for all containers |
| 55 | + env={"MY_VAR": "value"}, |
| 56 | + |
| 57 | + # Container daemon URL override (required for Colima/Podman Machine on macOS) |
| 58 | + container_host=None, |
| 59 | + |
| 60 | + # Base directory for job workspaces |
| 61 | + workdir_base=None, |
| 62 | +) |
| 63 | +``` |
| 64 | + |
| 65 | +### macOS-specific configuration |
| 66 | + |
| 67 | +On macOS, you may need to specify `container_host` depending on your container runtime: |
| 68 | + |
| 69 | +**Docker with Colima:** |
| 70 | +```python |
| 71 | +import os |
| 72 | +config = ContainerBackendConfig( |
| 73 | + container_host=f"unix://{os.path.expanduser('~')}/.colima/default/docker.sock" |
| 74 | +) |
| 75 | +``` |
| 76 | + |
| 77 | +**Podman Machine:** |
| 78 | +```python |
| 79 | +import os |
| 80 | +config = ContainerBackendConfig( |
| 81 | + container_host=f"unix://{os.path.expanduser('~')}/.local/share/containers/podman/machine/podman.sock" |
| 82 | +) |
| 83 | +``` |
| 84 | + |
| 85 | +**Docker Desktop:** |
| 86 | +```python |
| 87 | +# Usually works without specifying container_host |
| 88 | +config = ContainerBackendConfig() |
| 89 | +``` |
| 90 | + |
| 91 | +Alternatively, set environment variables before running: |
| 92 | +```bash |
| 93 | +# For Colima |
| 94 | +export DOCKER_HOST="unix://$HOME/.colima/default/docker.sock" |
| 95 | + |
| 96 | +# For Podman Machine |
| 97 | +export CONTAINER_HOST="unix://$HOME/.local/share/containers/podman/machine/podman.sock" |
| 98 | +``` |
| 99 | + |
| 100 | +### How it works |
| 101 | + |
| 102 | +The backend initialization follows this logic: |
| 103 | + |
| 104 | +1. If `runtime` is specified in config, use that runtime exclusively |
| 105 | +2. Otherwise, try to initialize Docker client adapter |
| 106 | +3. If Docker fails, try to initialize Podman client adapter |
| 107 | +4. If both fail, raise a RuntimeError |
| 108 | + |
| 109 | +If you don't have Docker or Podman installed, use `LocalProcessBackendConfig` instead, which runs training as local subprocesses. |
| 110 | + |
| 111 | +All container operations are delegated to the adapter, eliminating code duplication. |
| 112 | + |
| 113 | +## Installation |
| 114 | + |
| 115 | +Install with Docker support: |
| 116 | +```bash |
| 117 | +pip install kubeflow[docker] |
| 118 | +``` |
| 119 | + |
| 120 | +Install with Podman support: |
| 121 | +```bash |
| 122 | +pip install kubeflow[podman] |
| 123 | +``` |
| 124 | + |
| 125 | +Install with both: |
| 126 | +```bash |
| 127 | +pip install kubeflow[docker,podman] |
| 128 | +``` |
| 129 | + |
| 130 | +## Example: Training Job |
| 131 | + |
| 132 | +```python |
| 133 | +from kubeflow.trainer import TrainerClient, ContainerBackendConfig, CustomTrainer |
| 134 | + |
| 135 | +# Define your training function |
| 136 | +def train(): |
| 137 | + import torch |
| 138 | + print(f"Training with PyTorch {torch.__version__}") |
| 139 | + # Your training code here |
| 140 | + |
| 141 | +# Create trainer |
| 142 | +trainer = CustomTrainer( |
| 143 | + func=train, |
| 144 | + packages_to_install=["torch"], |
| 145 | +) |
| 146 | + |
| 147 | +# Initialize client (auto-detects runtime) |
| 148 | +config = ContainerBackendConfig() |
| 149 | +client = TrainerClient(backend_config=config) |
| 150 | + |
| 151 | +# Run training |
| 152 | +job_name = client.train(trainer=trainer) |
| 153 | +print(f"Training job started: {job_name}") |
| 154 | + |
| 155 | +# Get logs |
| 156 | +for log in client.get_job_logs(job_name, follow=True): |
| 157 | + print(log, end='') |
| 158 | +``` |
| 159 | + |
| 160 | +## See also |
| 161 | + |
| 162 | +- [Example notebook](TBA) - Complete working example to be added |
0 commit comments