Skip to main content

Minimal Docker-only sandbox for executing code

Project description

ContainerBox

Minimal Docker-only sandbox for running generated code.

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.0.tar.gz (5.5 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.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: containerbox-0.1.0.tar.gz
  • Upload date:
  • Size: 5.5 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.0.tar.gz
Algorithm Hash digest
SHA256 09ab1dd1d3e1cddb64274c431dfd0e2f1e161597a5e7a634462f805689ef94f6
MD5 1dc78866e5baf555aeabf0befe97061b
BLAKE2b-256 611523c76ce561c250f67a750344918d7329700a4ab51aafccf2f14e8be81c70

See more details on using hashes here.

Provenance

The following attestation bundles were made for containerbox-0.1.0.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.0-py3-none-any.whl.

File metadata

  • Download URL: containerbox-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f80547940c16a133697363e659202cb203caf8b5491473d9af7a2bfe66266eb8
MD5 e4c5a0ff00a2e97728d3dcb92841cbcd
BLAKE2b-256 cce78b1d1300a24987a364b6e00c866311b06326a83f5fd48a422922a26004aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for containerbox-0.1.0-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