docker

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 26 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package docker is the sandbox driver that runs each sandbox as a plain Docker container. It is for development only.

A container shares the host's kernel, so this driver reports sandbox.IsolationContainer, which kojo doctor refuses outside dev mode: use sbx or k8s to run code you do not trust. What the driver does provide is the rest of the security model, so that a development setup behaves like a real one:

  • Credentials never enter the container. The driver resolves a policy's credential references on the host and hands the values to kojo-proxy; the container only gets placeholders (KOJO_CRED_<NAME>), the proxy address, and the proxy's CA certificate.
  • Egress goes through kojo-proxy. Sandboxes join a Docker network created with --internal, which has no route to the outside and no external DNS. The only thing reachable on it is the proxy: directly, when the proxy is attached to that network (docker compose), or through a small relay container that forwards one port to the proxy on the host (`kojo serve` on a laptop). Health reports EgressEnforced truthfully: it is false when the configured network is not internal.
  • Containers run with no-new-privileges, without NET_RAW (no ARP spoofing of the proxy on the shared network), with a PID limit, and with the CPU and memory limits of the spec, lowered to Options.MaxResources and to what the Docker daemon has (docker info, read once): Docker refuses more CPUs than the daemon has.

Known limits, all acceptable for development and none for production: sandboxes on the same network can open connections to each other; the Disk resource is not enforced; a sandbox that needs its own Docker daemon is refused, because that takes a privileged container; and the driver runs a few fixed shell snippets as root inside the container (installing the CA bundle, killing a timed-out command), which trusts the image's sh and coreutils.

The driver shells out to the docker CLI. All state lives in Docker (container, volume, labels), so another process finds a sandbox again with Get; only the credential placeholders are kept in memory and return with the next ApplyPolicy.

An image must provide sh, sleep, true, id, chown, tar, tr, grep, sed, cut and kill: BusyBox or coreutils. Commands run as the image's user, and the workspace volume is handed to that user when the sandbox is created.

Index

Constants

View Source
const (
	// DefaultNetwork is the internal network the driver creates when
	// Options.Network is empty.
	DefaultNetwork = "kojo-sandbox"
	// DefaultRelayImage runs the relay between an internal network and a
	// proxy on the host. It must provide sh and socat.
	DefaultRelayImage = "alpine/socat:1.8.1.3"
	// DefaultReadyTimeout bounds the wait for a new container to accept an
	// Exec, image pull included.
	DefaultReadyTimeout = 5 * time.Minute
)

Defaults for Options.

View Source
const (
	LabelSandbox   = "kojo.sandbox"   // the sandbox ID
	LabelSession   = "kojo.session"   // SandboxSpec.SessionID
	LabelSkill     = "kojo.skill"     // SandboxSpec.Skill
	LabelExpires   = "kojo.expires"   // RFC 3339, absent without a TTL
	LabelWorkspace = "kojo.workspace" // where the workspace volume is mounted
	LabelNetwork   = "kojo.network"   // the sandbox network, which scopes Reap

)

Labels the driver sets on containers and volumes.

View Source
const (

	// CABundlePath is the bundle every Exec's TLS variables point at: the
	// image's system roots plus the proxy CA.
	CABundlePath = caDir + "/ca-bundle.crt"
)
View Source
const Name = "docker"

Name is the driver's key in kojo.yaml.

Variables

This section is empty.

Functions

This section is empty.

Types

type Driver

type Driver struct {
	// contains filtered or unexported fields
}

Driver runs sandboxes as Docker containers. It is safe for concurrent use.

func New

func New(opts Options) (*Driver, error)

New returns a driver. It does not talk to Docker: Health does.

func (*Driver) Create

func (d *Driver) Create(ctx context.Context, spec sandbox.SandboxSpec) (sandbox.Sandbox, error)

Create starts a container with a named volume as its workspace and waits until Exec works in it. The spec's policy is applied before it returns.

func (*Driver) Destroy

func (d *Driver) Destroy(ctx context.Context, id string) error

Destroy removes the container, its workspace volume and its proxy policy.

func (*Driver) Get

func (d *Driver) Get(ctx context.Context, id string) (sandbox.Sandbox, error)

Get finds a sandbox by ID, whichever process created it. A container that is stopped (the Docker daemon restarted) is started again: its workspace volume is still there.

func (*Driver) Health

func (d *Driver) Health(ctx context.Context) (sandbox.Health, error)

Health reports the Docker version and whether egress is enforced. It sets up the sandbox network (and the relay, when one is needed) to find out, as the first Create would.

func (*Driver) Name

func (d *Driver) Name() string

Name returns "docker".

func (*Driver) Reap

func (d *Driver) Reap(ctx context.Context) ([]string, error)

Reap destroys the sandboxes whose TTL has passed and returns their IDs. It goes by the kojo.expires label, so it also removes sandboxes another process created and left behind, and volumes whose container is gone. It only looks at sandboxes of this driver's network, so two kojo installs on one Docker daemon leave each other alone. The core calls it periodically.

func (*Driver) Teardown

func (d *Driver) Teardown(ctx context.Context) error

Teardown removes what the driver set up around sandboxes: the relay container of its network and, when the driver created the network and nothing is attached to it any more, the network. Sandboxes are not touched: Destroy or Reap them first. Tests use it; a deployment has no reason to.

type Options

type Options struct {
	// Binary is the docker CLI. Default "docker" on PATH.
	Binary string
	// Network is the Docker network sandboxes join. When empty the driver
	// creates DefaultNetwork as an internal network. A network named here
	// must exist; when it is not internal, egress is not enforced and Health
	// says so.
	Network string
	// Proxy is the kojo-proxy sandboxes go through. Required.
	Proxy proxy.Controller
	// Credentials resolves a policy's credential references. It may be nil
	// when no policy names a credential.
	Credentials sandbox.CredentialSource
	// Logger receives the driver's log lines. Default: discard.
	Logger *slog.Logger
	// RelayImage is the image of the relay container. Default
	// DefaultRelayImage.
	RelayImage string
	// ReadyTimeout bounds Create's wait for the container, image pull
	// included. Default DefaultReadyTimeout.
	ReadyTimeout time.Duration
	// MaxResources is the most CPU and memory a sandbox gets
	// (sandbox.max_resources). A spec that asks for more gets this much.
	// Independently of it, a request is lowered to the CPUs and memory the
	// Docker daemon has. Disk is not enforced by this driver.
	MaxResources sandbox.Resources
}

Options configure the driver.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL