Skip to main content

Minimal Docker-only sandbox for executing code

Project description

ContainerBox

Minimal docker based sandbox for running generated code and bash commands with uploading files, custom images and donwloading the results.

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

Create a Dockerfile in your own project:

FROM node:22-slim

WORKDIR /workspace

RUN npm init -y && npm install slugify

ENV NODE_PATH=/workspace/node_modules

Build it:

docker build -t my-node-sandbox:latest .

Use that image with ContainerBox:

from containerbox import SandboxSession

code = """
const slugify = require("slugify");
console.log(slugify("Hello from Custom Node Image!", { lower: true }));
"""

with SandboxSession("my-node-sandbox:latest") 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.2.tar.gz (5.8 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.2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: containerbox-0.1.2.tar.gz
  • Upload date:
  • Size: 5.8 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.2.tar.gz
Algorithm Hash digest
SHA256 a87e383d8e65f58ae30576df5d6545a3e727878c2f0477b84a74eff88968166e
MD5 bc1f93470bc4000a8c34bae2b57ecbd8
BLAKE2b-256 726d72e5fc57a1b64392992efe7cff1a95979d374279bf5e1d0dbd41b2eb38c4

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: containerbox-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.6 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e46b0027c076d91d145bffb505cb5dc8daf213ffdc742ad9f3f2d9c620274d91
MD5 021e75bad2e144e14303b49df47d2474
BLAKE2b-256 57ec148b7436d619133378ec755ac115c9c66f5760843a9f7bd72ec11bd60324

See more details on using hashes here.

Provenance

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