|
| 1 | +# Quick start |
| 2 | + |
| 3 | +## Prerequisites |
| 4 | + |
| 5 | +**A Docker daemon**, and **gVisor (`runsc`) registered with it**. |
| 6 | + |
| 7 | +openblox will not run a sandbox without gVisor. It does not fall back to `runc`, and |
| 8 | +that refusal is deliberate: falling back would silently run untrusted code on the host |
| 9 | +kernel while the API kept reporting success. |
| 10 | + |
| 11 | +Install gVisor per the [official instructions](https://gvisor.dev/docs/user_guide/install/), |
| 12 | +then register it: |
| 13 | + |
| 14 | +```json title="/etc/docker/daemon.json" |
| 15 | +{ |
| 16 | + "runtimes": { |
| 17 | + "runsc": { |
| 18 | + "path": "/usr/bin/runsc" |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +```sh |
| 25 | +sudo systemctl reload docker |
| 26 | +docker info --format '{{json .Runtimes}}' | grep runsc # confirm |
| 27 | +``` |
| 28 | + |
| 29 | +If `runsc` is absent, `Create` returns an error wrapping `sandbox.ErrRuntimeUnavailable` |
| 30 | +— branch on that if you want to degrade gracefully rather than fail. |
| 31 | + |
| 32 | +## Install |
| 33 | + |
| 34 | +```sh |
| 35 | +go get github.com/blox-eng/openblox |
| 36 | +``` |
| 37 | + |
| 38 | +## Run something |
| 39 | + |
| 40 | +```go |
| 41 | +package main |
| 42 | + |
| 43 | +import ( |
| 44 | + "context" |
| 45 | + "fmt" |
| 46 | + "log" |
| 47 | + |
| 48 | + "github.com/blox-eng/openblox/pkg/docker" |
| 49 | + "github.com/blox-eng/openblox/pkg/sandbox" |
| 50 | +) |
| 51 | + |
| 52 | +func main() { |
| 53 | + ctx := context.Background() |
| 54 | + |
| 55 | + backend, err := docker.New() |
| 56 | + if err != nil { |
| 57 | + log.Fatal(err) |
| 58 | + } |
| 59 | + defer backend.Close() |
| 60 | + |
| 61 | + sb, err := backend.Create(ctx, "session-1", |
| 62 | + sandbox.WithImage("ghcr.io/blox-eng/openblox-sandbox:latest")) |
| 63 | + if err != nil { |
| 64 | + log.Fatal(err) |
| 65 | + } |
| 66 | + defer backend.Destroy(ctx, "session-1") |
| 67 | + |
| 68 | + res, err := sb.Exec(ctx, sandbox.Command{ |
| 69 | + Argv: []string{"python3", "-c", "print(6 * 7)"}, |
| 70 | + }) |
| 71 | + if err != nil { |
| 72 | + log.Fatal(err) |
| 73 | + } |
| 74 | + fmt.Println(string(res.Stdout)) // 42 |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +`Create` is keyed by name: calling it again with the same name returns the existing |
| 79 | +sandbox rather than a second one. That makes it safe to call per request without |
| 80 | +tracking what already exists. |
| 81 | + |
| 82 | +A non-zero `res.ExitCode` is the program failing, not an openblox error. `err` is |
| 83 | +reserved for openblox failing to run it at all. |
| 84 | + |
| 85 | +!!! note "Argv, not a shell string" |
| 86 | + `Command.Argv` is passed directly to `exec`. Nothing in it is parsed as shell |
| 87 | + syntax, so a caller cannot accidentally create an injection by interpolating |
| 88 | + untrusted text into a command line. |
| 89 | + |
| 90 | +## Files |
| 91 | + |
| 92 | +```go |
| 93 | +err := sb.WriteFile(ctx, "/workspace/data.csv", 0o644, strings.NewReader("a,b\n1,2\n")) |
| 94 | + |
| 95 | +rc, err := sb.ReadFile(ctx, "/workspace/out.json") |
| 96 | +defer rc.Close() |
| 97 | +body, err := io.ReadAll(rc) |
| 98 | +``` |
| 99 | + |
| 100 | +Both stream, so they are safe for large payloads. Paths are absolute inside the |
| 101 | +sandbox; `/workspace` is the working directory and is writable. |
| 102 | + |
| 103 | +## Background processes |
| 104 | + |
| 105 | +```go |
| 106 | +err := sb.StartProcess(ctx, "web", sandbox.Command{ |
| 107 | + Argv: []string{"python3", "-m", "http.server", "8080", "--bind", "127.0.0.1"}, |
| 108 | +}) |
| 109 | +``` |
| 110 | + |
| 111 | +Idempotent: if something is already running under that name, it is left alone and no |
| 112 | +error is returned. Call it on every request rather than tracking state yourself. |
| 113 | + |
| 114 | +## Preview links |
| 115 | + |
| 116 | +A sandbox has **no network interface**, so a port inside it is not reachable by any |
| 117 | +ordinary route. openblox reaches it over the exec channel and fronts it with a signed, |
| 118 | +expiring URL. |
| 119 | + |
| 120 | +```go |
| 121 | +backend, err := docker.New( |
| 122 | + docker.WithPreviews(signingKey, "https://example.com"), // key >= 32 random bytes |
| 123 | +) |
| 124 | + |
| 125 | +// Mount the handler where the signed URLs will resolve. |
| 126 | +http.Handle(preview.RoutePrefix+"/", backend.PreviewHandler()) |
| 127 | + |
| 128 | +p, err := sb.Expose(ctx, 8080, 10*time.Minute) |
| 129 | +// p.URL + p.Token — send the token as an Authorization header, never a query param. |
| 130 | +``` |
| 131 | + |
| 132 | +!!! warning "Revocation is best-effort; expiry is the guarantee" |
| 133 | + Verification is a local HMAC check that consults no shared state, so `Revoke` only |
| 134 | + holds in the process that recorded it. If you run several replicas, treat the TTL |
| 135 | + as the real bound and keep it short. |
| 136 | + |
| 137 | +## Cleaning up |
| 138 | + |
| 139 | +```go |
| 140 | +removed, err := backend.Reap(ctx) // destroys sandboxes past idle timeout or max age |
| 141 | +``` |
| 142 | + |
| 143 | +Call it from a ticker. It is safe to run concurrently with everything else, and safe to |
| 144 | +run from several processes at once. |
| 145 | + |
| 146 | +## Defaults |
| 147 | + |
| 148 | +A sandbox created with no options gets: |
| 149 | + |
| 150 | +| | | |
| 151 | +|---|---| |
| 152 | +| Runtime | `runsc` (gVisor) | |
| 153 | +| Network | none | |
| 154 | +| User | `1000:1000` (non-root) | |
| 155 | +| Root filesystem | read-only | |
| 156 | +| CPUs | 2 | |
| 157 | +| Memory | 2 GiB | |
| 158 | +| Scratch disk | 1 GiB (tmpfs, drawn **from** the memory budget) | |
| 159 | +| Max processes | 256 | |
| 160 | +| Idle timeout | 15 minutes | |
| 161 | +| Max age | 2 hours | |
| 162 | +| Command timeout | 60s default, 10m ceiling | |
| 163 | + |
| 164 | +Scratch space is tmpfs, so it comes out of memory — keep disk at or below memory, and |
| 165 | +size both deliberately if your workload is heavy. |
0 commit comments