Skip to main content

dr-docker

Reusable Docker execution contracts and adapters.

Purpose

This repo provides Docker runtime contracts and a concrete subprocess adapter:

  • Docker runtime request/result contracts with security and resource profiles
  • Runtime adapter protocol
  • Subprocess-based Docker adapter with stream capping and cidfile cleanup
  • Worker helpers for mounted scripts/directories with reusable runtime policies
  • Typed error envelopes

Public Surface

from dr_docker import (
    CONTRACT_VERSION,
    DockerMount,
    DockerRuntimeRequest,
    DockerRuntimeResult,
    ErrorCode,
    ErrorEnvelope,
    MountedWorker,
    ResourceLimits,
    RuntimeAdapter,
    RuntimePrimitiveError,
    SecurityProfile,
    SubprocessDockerAdapter,
    TmpfsMount,
    WorkerRuntimePolicy,
    __version__,
    build_mounted_worker_request,
    execute_batch_in_container,
    mount_worker_directory,
    mount_worker_file,
    run_batch_with_failure_isolation,
)

Worker Support

dr-docker now includes a small worker-support layer for the common pattern of:

  • starting from a reusable isolated runtime policy
  • mounting a local worker file or directory into the container
  • building a DockerRuntimeRequest with stdin, env, mounts, tmpfs, and resource limits already wired together
from pathlib import Path

from dr_docker import (
    JsonWorkerExecutionConfig,
    WorkerRuntimePolicy,
    build_mounted_worker_request,
    mount_worker_file,
    parse_byte_size,
)

worker = mount_worker_file(Path("worker.py"), mount_target="/sandbox")
worker = worker.with_path_command(
    entrypoint="python3",
    args_before_path=["-I"],
    working_dir="/tmp",
)

policy = WorkerRuntimePolicy.small_isolated().with_env_overrides().model_copy(
    update={"memory": "1g", "tmpfs_exec": True}
)
worker_config = JsonWorkerExecutionConfig.from_runtime_policy(
    policy,
    timeout_seconds=30,
)

request = build_mounted_worker_request(
    image="python:3.12-slim",
    worker=worker,
    timeout_seconds=30,
    policy=policy,
    stdin_payload='{"job": "ping"}',
    env={
        **worker_config.to_env(),
        "WORKER_MODE": "json",
    },
)

For optional worker-side JSON-over-stdin helpers, use dr_docker.workers.json_stdio. That module intentionally stays separate from the core Docker contract layer and includes bounded stdin reading, bounded stdout capture, container guards, and basic RLIMIT helpers.

For reusable size parsing, use parse_byte_size("1.5g").

Contract Guarantees

  • DockerRuntimeResult(ok=False) requires error
  • Successful result envelopes must not include error
  • Error envelopes are typed (ErrorCode) with non-empty message and JSON-safe details
  • Supported ErrorCode values are timeout, unavailable, and internal_error

Development

uv sync --group dev
uv run pytest -q
uv run ruff format --check
uv run ruff check
uv run ty check

Publishing

cp .env.example .env
# set PYPI_API_TOKEN in .env
set -a; source .env; set +a
uv build
uvx twine check dist/*
uvx twine upload -u __token__ -p "$PYPI_API_TOKEN" dist/*

Metadata

Release files for dr-docker 0.4.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dr-docker 0.4.5
File Size Uploaded
dr_docker-0.4.5.tar.gz 54.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dr-docker 0.4.5
File Interpreter ABI Platform
dr_docker-0.4.5-py3-none-any.whl Python 3 none any Details

Total release size: 75.8 kB

Release files / dr_docker-0.4.5.tar.gz

Download URL dr_docker-0.4.5.tar.gz
Size 54.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c2ee6aba4297de97e3c679137e41b647939ff9e8b164682369a608e955485146
BLAKE2b-256 checksum
How to use checksums
513a4d2d113d7b1cd15c60ce0f3cf52679647609630ee714b3e9f82a9f0a839b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.0

Release files / dr_docker-0.4.5-py3-none-any.whl

Download URL dr_docker-0.4.5-py3-none-any.whl
Size 21.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2e268eb7c69ead188ecdec021f2b853e01741e626f99a00748953ebb6e42b753
BLAKE2b-256 checksum
How to use checksums
34080d6e873c862c136f6a86f2c4ae66116036eaf3367ab6c83c3dec630ff9c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.0

Release history Release notifications | RSS feed

This release

0.4.5 This release

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page