sbx

package module
v0.45.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package sbx registers a Docker Sandbox runtime for Testcontainers. The sbx CLI must be installed and authenticated with sbx login before provider initialization. Importing the package does not start a sandbox. The sandbox's Docker API is unauthenticated on host loopback; use a host with trusted local users and processes.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrProviderStarted = errors.New("sbx provider initialization has started; configure before the first Testcontainers operation")

ErrProviderStarted is returned when configuration is attempted after provider initialization has begun, including after failed initialization or cleanup.

Functions

func Cleanup

func Cleanup(ctx context.Context) error

Cleanup removes the sandbox managed by the default provider. It does nothing if initialization has not started.

func Configure

func Configure(opts ...Option) error

Configure configures the registered default provider without starting or selecting it. Call it before any Testcontainers operation, typically in TestMain before m.Run or provider health checks. Select sbx separately through TESTCONTAINERS_PROVIDER or the provider property. Each call applies all its options atomically or leaves configuration unchanged. Explicit settings override TESTCONTAINERS_SBX_* environment variables, which are read when initialization begins.

Example

These examples omit Output directives so they compile without changing the process-wide provider configuration used by the integration tests.

package main

import (
	"log"
	"time"

	"github.com/testcontainers/testcontainers-go/modules/sbx"
)

func main() {
	// configureProvider {
	// Save the documented example kit as testdata/test-tools/test-tools.yaml first.
	if err := sbx.Configure(
		sbx.WithStartupTimeout(8*time.Minute),
		sbx.WithKits("./testdata/test-tools"),
		sbx.WithKitArgs(map[string]string{
			"test-tools.mode": "integration",
		}),
	); err != nil {
		log.Fatal(err)
	}
	// }
}

Types

type Option

type Option func(*options) error

Option configures a sandbox before provider initialization. These options apply to the sandbox, not to containers running inside it.

func WithAppName

func WithAppName(name string) Option

WithAppName sets the sbx CLI namespace for every command, overriding TESTCONTAINERS_SBX_APP_NAME. An empty name selects the CLI's default namespace. Names accept up to 20 ASCII letters, digits, hyphens, and underscores. Authenticate the selected namespace with sbx login before initialization.

Example
package main

import (
	"log"

	"github.com/testcontainers/testcontainers-go/modules/sbx"
)

func main() {
	// withAppName {
	if err := sbx.Configure(sbx.WithAppName("tc-tests")); err != nil {
		log.Fatal(err)
	}
	// }
}

func WithBinary

func WithBinary(binary string) Option

WithBinary sets the sbx executable path or name, overriding TESTCONTAINERS_SBX_BINARY. The executable is resolved when initialization begins.

Example
package main

import (
	"log"

	"github.com/testcontainers/testcontainers-go/modules/sbx"
)

func main() {
	// withBinary {
	if err := sbx.Configure(sbx.WithBinary("/usr/local/bin/sbx")); err != nil {
		log.Fatal(err)
	}
	// }
}

func WithBindRoots

func WithBindRoots(paths ...string) Option

WithBindRoots replaces the additional allowed host bind roots, overriding TESTCONTAINERS_SBX_BIND_ROOTS and any earlier WithBindRoots option. Paths must be absolute directories that exist at startup. WithBindRoots() clears additional roots; the workspace is always allowed. Input paths are copied.

Example
package main

import (
	"log"

	"github.com/testcontainers/testcontainers-go/modules/sbx"
)

func main() {
	// withBindRoots {
	if err := sbx.Configure(sbx.WithBindRoots("/opt/test-fixtures", "/srv/shared-fixtures")); err != nil {
		log.Fatal(err)
	}
	// }
}

func WithKitArgs

func WithKitArgs(args map[string]string) Option

WithKitArgs merges kit arguments. Later values override earlier values for the same key. Use "name" for a shared argument or "kit-handle.name" to target one kit. sbx validates declared arguments and their values. Empty values are valid. Arguments are plain text recorded by sbx; use sbx credentials for secrets.

Example
package main

import (
	"log"

	"github.com/testcontainers/testcontainers-go/modules/sbx"
)

func main() {
	// withKitArgs {
	if err := sbx.Configure(sbx.WithKitArgs(map[string]string{
		"test-tools.mode": "integration",
	})); err != nil {
		log.Fatal(err)
	}
	// }
}

func WithKits

func WithKits(refs ...string) Option

WithKits appends mixin references to the sandbox's creation request, in order. The required Docker TCP kit is always included automatically. sbx validates source references, duplicate kits, dependencies, and compatibility. References may be OCI images, local directories, or Git sources. Relative paths are resolved by the CLI against the process working directory at startup, independently of WithWorkspace or TESTCONTAINERS_SBX_WORKSPACE.

Example
package main

import (
	"log"

	"github.com/testcontainers/testcontainers-go/modules/sbx"
)

func main() {
	// withKits {
	// Save the documented example kit as testdata/test-tools/test-tools.yaml first.
	if err := sbx.Configure(sbx.WithKits("./testdata/test-tools")); err != nil {
		log.Fatal(err)
	}
	// }
}

func WithStartupTimeout

func WithStartupTimeout(timeout time.Duration) Option

WithStartupTimeout sets the positive timeout for sandbox initialization, including kit installation, overriding TESTCONTAINERS_SBX_TIMEOUT.

Example
package main

import (
	"log"
	"time"

	"github.com/testcontainers/testcontainers-go/modules/sbx"
)

func main() {
	// withStartupTimeout {
	if err := sbx.Configure(sbx.WithStartupTimeout(8 * time.Minute)); err != nil {
		log.Fatal(err)
	}
	// }
}

func WithWorkspace

func WithWorkspace(path string) Option

WithWorkspace sets the sandbox workspace, overriding TESTCONTAINERS_SBX_WORKSPACE. Relative paths are resolved against the process working directory at startup. The directory must exist when initialization begins.

Example
package main

import (
	"log"

	"github.com/testcontainers/testcontainers-go/modules/sbx"
)

func main() {
	// withWorkspace {
	if err := sbx.Configure(sbx.WithWorkspace(".")); err != nil {
		log.Fatal(err)
	}
	// }
}

type Provider

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

Provider manages one Docker Sandbox. Its zero value is ready for use.

func (*Provider) Cleanup

func (p *Provider) Cleanup(ctx context.Context) error

Cleanup waits for any active initialization and removes this provider's sandbox. It does nothing before initialization and can be retried after failure.

func (*Provider) Configure

func (p *Provider) Configure(opts ...Option) error

Configure configures this provider without starting it. Repeated calls compose until initialization begins; later calls return ErrProviderStarted. Each call applies all its options atomically or leaves configuration unchanged.

func (*Provider) Initialize

func (p *Provider) Initialize(ctx context.Context) (testcontainers.RuntimeConfig, error)

Initialize starts a sandbox and returns its runtime configuration. The sbx CLI must already be authenticated with sbx login. Host binds are limited to the workspace and absolute directories configured with WithBindRoots or TESTCONTAINERS_SBX_BIND_ROOTS. Initialization is attempted once per Provider.

Jump to

Keyboard shortcuts

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