Run Python snippets inside a tightly constrained Docker container from Discord
- Per-user sandboxes: one sandbox directory per user, persisted on disk and auto-expiring after 1 week of inactivity.
- Slash commands under
/il:/il create: create your sandbox./il py: run Python code inside your sandbox (30s timeout)./il look [path]: change/list current directory, with pagination./il write name content: create/overwrite a file./il rm name [recursive]: delete a file or a directory (withrecursive=true)./il pip packages:"...": install Python packages into your sandbox with throttled log updates./il delete: delete your sandbox and all files.
- Secure execution in
python:3.11-alpinewith strict limits:- No network for code runs (
--network none), read-only FS, tmpfs/tmp - Non-root user, drop all caps,
no-new-privileges - CPU, memory, and pids limits; GPU not used
- 30s execution timeout per
/il py
- No network for code runs (
- Package installs:
/il pipallows network access only for installing to/workspace/.site-packages; logs are edited at most every 3 seconds to avoid Discord rate limits. - Long outputs are truncated; when too large, the bot attaches the full output as a file.
- Docker Desktop (or Docker Engine) running and accessible as
docker - Python 3.9+
- A Discord Bot token
-
Create and activate a virtual environment (optional but recommended):
python -m venv .venv . .venv/bin/activate # Windows: .venv\\Scripts\\activate
-
Install dependencies:
pip install -r requirements.txt
-
Set your Discord bot token:
# PowerShell $env:DISCORD_TOKEN = "YOUR_TOKEN_HERE" # bash/zsh export DISCORD_TOKEN="YOUR_TOKEN_HERE"
-
Run the bot:
python bot.py
- Create your sandbox
/il create
- (Optional) Install packages into your sandbox
/il pip packages:"requests numpy"
- Run code in your sandbox (code block or inline)
/il py code:"""
```python
import requests
print("requests:", requests.__version__)
- Browse/change directory with pagination
/il look # list current directory
/il look path:subdir # cd into subdir and list
- Manage files
/il write name:main.py content:"print('hi')"
/il rm name:main.py
/il rm name:folder recursive:true
- Delete your sandbox
/il delete
Notes
/il pyrespects a 30s timeout, disables network, and limits CPU/RAM./il pipallows network only for installation, stores packages in/workspace/.site-packages, and/il pysetsPYTHONPATHaccordingly.
Environment variables (optional):
SANDBOX_IMAGE: Docker image (defaultpython:3.11-alpine)SANDBOX_PULL_ON_STARTUP: pre-pull image on startup (default1)IL_BASE_DIR: host directory for sandboxes (default./il_sandboxes)IL_TIMEOUT_SECONDS:/il pytimeout (default30.0)IL_MEMORY: memory limit for runs and pip (default256m)IL_CPUS: CPU limit for runs and pip (default1.0)IL_RETENTION_SECONDS: sandbox expiry in seconds (default604800)ECHO_LAST_EXPR: REPL-style echo of last expression (default1)DOCKER_BINARY: docker binary name/path (defaultdocker)
Resource limits are enforced in sandbox.py (--network none for runs, --cpus, --memory, --pids-limit, non-root user). Adjust via env vars where exposed.
If you see Execution timed out on the very first run, Docker was likely pulling the Python image and exceeded the short execution timeout. Either let the bot pre-pull (default), or run:
docker pull python:3.11-alpine
Then retry. Use /health to check whether Docker is reachable and the image is present.
This design aims for strong isolation for untrusted snippets using Docker. Still, treat it as best-effort isolation and avoid running the bot on hosts with sensitive data or elevated privileges. Keep Docker updated and prefer Linux containers.