Skip to main content

Minimal Docker-only sandbox for executing code

Project description

ContainerBox

Minimal Docker-only sandbox for running generated code.

Missing images are pulled automatically before the sandbox is created.

Usage

Use the context manager when the sandbox belongs to one block of work:

from containerbox import SandboxSession

with SandboxSession() as session:
    result = session.exec("echo hi")
    print(result.stdout)

Use manual lifecycle when you need to pass the same sandbox across functions or modules:

from containerbox import SandboxSession

session = SandboxSession("python:3.13-slim")
session.open()

try:
    result = session.run_code("print('hi')", timeout=5)
    print(result.stdout)
finally:
    session.close()

When using manual lifecycle, always call close(). The context manager does this for you; manual mode makes cleanup your responsibility.

API

with SandboxSession(
    image="ubuntu:24.04",
    runtime="docker",
    session_timeout=300,
    memory="256m",
    cpus=1.0,
    network=False,
) as session:
    result = session.exec("echo ready", timeout=10)

For Python code, use a Python image:

with SandboxSession("python:3.13-slim") as session:
    result = session.run_code("print('hi')", timeout=5)

SandboxResult contains:

  • stdout
  • stderr
  • exit_code
  • timed_out
  • duration_ms

Files

with SandboxSession("python:3.13-slim") as session:
    session.upload("local_data.csv")
    result = session.run_code("print(open('local_data.csv').read())")
    session.download("main.py", "downloaded_main.py")

Custom Image

docker build -f tests/Dockerfile.node -t containerbox-node-extra:test .
from containerbox import SandboxSession

code = """
const { slug } = require("slugify-mini");
console.log(slug("Hello from Custom Node Image!"));
"""

with SandboxSession("containerbox-node-extra:test") as session:
    result = session.run_code(code, filename="main.js", command=["node", "main.js"])
    print(result.stdout)

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

containerbox-0.1.1.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

containerbox-0.1.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file containerbox-0.1.1.tar.gz.

File metadata

  • Download URL: containerbox-0.1.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for containerbox-0.1.1.tar.gz
Algorithm Hash digest
SHA256 320c53313f31cb64ce6d0118afd021a10f965d08198c683725067bc3d5e3d295
MD5 e09cc09028b425013c71d5f594737f21
BLAKE2b-256 07aba5015892b1d5fa33eaa29955b9a6cb1bbaffef216a6d45cd6c7cea4f90ab

See more details on using hashes here.

Provenance

The following attestation bundles were made for containerbox-0.1.1.tar.gz:

Publisher: publish.yml on pushpitkamboj/containerbox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file containerbox-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: containerbox-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for containerbox-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bdc6931d346b669791d1d357b0350abc8fdbb260955085a7cc7eebaae8d65cc2
MD5 cfcbce5a97b1259957e423663ef96568
BLAKE2b-256 4bb7a9a1e68d0108f00775483f8cf33f5a84777ed2530c68dca589eee63fc649

See more details on using hashes here.

Provenance

The following attestation bundles were made for containerbox-0.1.1-py3-none-any.whl:

Publisher: publish.yml on pushpitkamboj/containerbox

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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