Skip to main content

Container management library with backend abstraction for sandboxed execution

Project description

Podkit - Simple Container Management Library

A Python library for sandboxed execution in Docker containers with backend abstraction (Kubernetes-ready)

Features

Podkit implements a clean three-layer architecture for flexible container management: Layer 1 (Backend) provides runtime-agnostic infrastructure operations for Docker/Kubernetes with image management and workload execution; Layer 2 (ContainerManager) bridges infrastructure and application logic with container lifecycle management, project-specific mounting strategies, and host-to-container path translation; Layer 3 (SessionManager) delivers the user-facing API with session lifecycle tracking, automatic activity monitoring, and cleanup of expired sessions. This separation enables backend portability (swap Docker for Kubernetes without touching business logic), customizable project configurations (different mounting strategies per project), and independent testing of each layer.

Example

from pathlib import Path
from podkit.backends.docker import DockerBackend
from podkit.core.manager import BaseContainerManager
from podkit.core.session import BaseSessionManager
from podkit.core.models import ContainerConfig

backend = DockerBackend()
container_manager = BaseContainerManager(
    backend=backend,
    container_prefix="podkit",
    workspace_base="/tmp/podkit_workspace"
)
session_manager = BaseSessionManager(
    container_manager=container_manager,
    default_image="python:3.15-rc-alpine3.23"
)

sandbox_config = ContainerConfig(
    image=session_manager.default_image,
    entrypoint=[],
)

session = session_manager.create_session(
    user_id="user",
    session_id="session",
    config=sandbox_config,
)

result = session_manager.execute_command(
    user_id="user",
    session_id="session",
    command=["sh", "-c", "echo 'Hello'"],
    working_dir=None,
)

session_manager.write_file(
    user_id="user",
    session_id="session",
    container_path=Path("/workspace") / "file.txt",
    content="Hello",
)

# Option 1: Manual cleanup - explicitly close the session
session_manager.close_session("user", "session")

# Option 2: Automatic cleanup - set timeout for auto-expiration after idle period
# Configure container to auto-expire after 30 minutes of inactivity
sandbox_config_with_timeout = ContainerConfig(
    image="python:3.15-rc-alpine3.23",
    entrypoint=[],
    timeout_seconds=1800,  # 30 minutes in seconds
)

session_with_timeout = session_manager.create_session(
    user_id="user2",
    session_id="session2",
    config=sandbox_config_with_timeout,
)

# Session will automatically be marked as expired after 30 minutes of inactivity
# Check if session has expired
is_expired = session_with_timeout.is_expired()  # Returns True if idle > 30 minutes

Development Setup

Prerequisites

  • Docker
  • uv

Installation

./scripts/install.sh

Running Tests

Integration Tests (Recommended)

Run tests in Docker container (most realistic):

./scripts/test.sh

This will:

  1. Build the test runner container with all dependencies
  2. Mount the Docker socket and test workspace
  3. Run pytest with the integration tests
  4. Clean up automatically

Local Testing (Development)

For faster iteration during development:

# Start test container and keep it running
docker-compose run --rm test-runner bash

# Inside the container, run tests
pytest tests/integration/test_integration_happy_path.py -v

# Or run specific tests
pytest tests/integration/test_integration_happy_path.py::TestPodkitIntegrationHappyPath::test_01_backend_initialized -v

Linting

./scripts/lint.sh         # Check only
./scripts/lint.sh --fix   # Auto-fix issues

This runs ruff and pylint for code checking and formatting.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

podkit-0.2.1.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

podkit-0.2.1-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file podkit-0.2.1.tar.gz.

File metadata

  • Download URL: podkit-0.2.1.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for podkit-0.2.1.tar.gz
Algorithm Hash digest
SHA256 bc9b4d030c2ceb73ec3f88cab6bc283f22f7aeb42c88638201363e528cee1e6b
MD5 f481eedfc9e29bdabb44bf59dfa281c4
BLAKE2b-256 a5822ea8b00c2f54a4769cd702630d8e0d65f33db0cd36249b68039c265af3dd

See more details on using hashes here.

File details

Details for the file podkit-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: podkit-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for podkit-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fc5bac677e2be72e41f41a2761480b4146da1c8aaa128f10d01c1ec55e40e482
MD5 5281556e44dfaef98a2ebc7e06b6d1cf
BLAKE2b-256 bcd67b87084de27835d4faa6cc4171e23511b1ac6cf9410fc3ab7014d997554d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page