-
Notifications
You must be signed in to change notification settings - Fork 41
feat: Add ContainerBackend with Docker and Podman #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Fiona-Waters
wants to merge
6
commits into
kubeflow:main
Choose a base branch
from
Fiona-Waters:podman-backend
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1188510
Add docker backend
briangallagher 60fd258
Add podman backend
Fiona-Waters d1288b2
Implementing ContainerBackend
Fiona-Waters 0680c3e
Use ip address for Podman
Fiona-Waters 24f0bbd
Updating to rely on container runtime rather than storing job info in…
Fiona-Waters a9218c5
Addressing feedback
Fiona-Waters File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| # ContainerBackend | ||
|
||
|
|
||
| The unified container backend for Kubeflow Trainer that automatically detects and uses either Docker or Podman. | ||
|
|
||
| ## Overview | ||
|
|
||
| This backend provides a single, unified interface for container-based training execution, automatically detecting which container runtime is available on your system. | ||
|
|
||
| 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. | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Basic usage (auto-detection) | ||
|
|
||
| ```python | ||
| from kubeflow.trainer import TrainerClient, ContainerBackendConfig | ||
|
|
||
| # Auto-detects Docker or Podman | ||
| config = ContainerBackendConfig() | ||
| client = TrainerClient(backend_config=config) | ||
| ``` | ||
|
|
||
| ### Force specific runtime | ||
|
|
||
| ```python | ||
| # Force Docker | ||
| config = ContainerBackendConfig(runtime="docker") | ||
| client = TrainerClient(backend_config=config) | ||
|
|
||
| # Force Podman | ||
| config = ContainerBackendConfig(runtime="podman") | ||
| client = TrainerClient(backend_config=config) | ||
| ``` | ||
|
|
||
| ### Configuration options | ||
|
|
||
| ```python | ||
| config = ContainerBackendConfig( | ||
| # Optional: force specific runtime ("docker" or "podman") | ||
| runtime=None, | ||
|
|
||
| # Optional: explicit image override | ||
| image="my-custom-image:latest", | ||
|
|
||
| # Image pull policy: "IfNotPresent", "Always", or "Never" | ||
| pull_policy="IfNotPresent", | ||
|
|
||
| # Auto-remove containers and networks on job deletion | ||
| auto_remove=True, | ||
|
|
||
| # GPU support (varies by runtime) | ||
| gpus=None, | ||
|
|
||
| # Environment variables for all containers | ||
| env={"MY_VAR": "value"}, | ||
|
|
||
| # Container daemon URL override (required for Colima/Podman Machine on macOS) | ||
| container_host=None, | ||
|
|
||
| # Base directory for job workspaces | ||
| workdir_base=None, | ||
| ) | ||
| ``` | ||
|
|
||
| ### macOS-specific configuration | ||
|
|
||
| On macOS, you may need to specify `container_host` depending on your container runtime: | ||
|
|
||
| **Docker with Colima:** | ||
| ```python | ||
| import os | ||
| config = ContainerBackendConfig( | ||
| container_host=f"unix://{os.path.expanduser('~')}/.colima/default/docker.sock" | ||
| ) | ||
| ``` | ||
|
|
||
| **Podman Machine:** | ||
| ```python | ||
| import os | ||
| config = ContainerBackendConfig( | ||
| container_host=f"unix://{os.path.expanduser('~')}/.local/share/containers/podman/machine/podman.sock" | ||
| ) | ||
| ``` | ||
|
|
||
| **Docker Desktop:** | ||
| ```python | ||
| # Usually works without specifying container_host | ||
| config = ContainerBackendConfig() | ||
| ``` | ||
|
|
||
| Alternatively, set environment variables before running: | ||
| ```bash | ||
| # For Colima | ||
| export DOCKER_HOST="unix://$HOME/.colima/default/docker.sock" | ||
|
|
||
| # For Podman Machine | ||
| export CONTAINER_HOST="unix://$HOME/.local/share/containers/podman/machine/podman.sock" | ||
| ``` | ||
|
|
||
| ### How it works | ||
|
|
||
| The backend initialization follows this logic: | ||
|
|
||
| 1. If `runtime` is specified in config, use that runtime exclusively | ||
| 2. Otherwise, try to initialize Docker client adapter | ||
| 3. If Docker fails, try to initialize Podman client adapter | ||
| 4. If both fail, raise a RuntimeError | ||
|
|
||
| If you don't have Docker or Podman installed, use `LocalProcessBackendConfig` instead, which runs training as local subprocesses. | ||
|
|
||
| All container operations are delegated to the adapter, eliminating code duplication. | ||
|
|
||
| ## Installation | ||
|
|
||
| Install with Docker support: | ||
| ```bash | ||
| pip install kubeflow[docker] | ||
| ``` | ||
|
|
||
| Install with Podman support: | ||
| ```bash | ||
| pip install kubeflow[podman] | ||
| ``` | ||
|
|
||
| Install with both: | ||
| ```bash | ||
| pip install kubeflow[docker,podman] | ||
| ``` | ||
|
|
||
| ## Example: Training Job | ||
|
|
||
| ```python | ||
| from kubeflow.trainer import TrainerClient, ContainerBackendConfig, CustomTrainer | ||
|
|
||
| # Define your training function | ||
| def train(): | ||
| import torch | ||
| print(f"Training with PyTorch {torch.__version__}") | ||
| # Your training code here | ||
|
|
||
| # Create trainer | ||
| trainer = CustomTrainer( | ||
| func=train, | ||
| packages_to_install=["torch"], | ||
| ) | ||
|
|
||
| # Initialize client (auto-detects runtime) | ||
| config = ContainerBackendConfig() | ||
| client = TrainerClient(backend_config=config) | ||
|
|
||
| # Run training | ||
| job_name = client.train(trainer=trainer) | ||
| print(f"Training job started: {job_name}") | ||
|
|
||
| # Get logs | ||
| for log in client.get_job_logs(job_name, follow=True): | ||
| print(log, end='') | ||
| ``` | ||
|
|
||
| ## See also | ||
|
|
||
| - [Example notebook](TBA) - Complete working example to be added | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Copyright 2025 The Kubeflow Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from kubeflow.trainer.backends.container.backend import ContainerBackend | ||
| from kubeflow.trainer.backends.container.types import ContainerBackendConfig | ||
|
|
||
| __all__ = ["ContainerBackend", "ContainerBackendConfig"] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also include docs about Trainer local execution to the user guides ?
https://www.kubeflow.org/docs/components/trainer/user-guides/
you can also add info from the @szaher PR: #95
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WIP PR for this kubeflow/website#4221