Container-use provides isolated, containerized development environments for AI agents by combining:
- Git branches for each environment (e.g.,
adjective-animalnames likefluffy-lion) - Dagger containers with code mounted at
/workdir - Git notes (
container-useandcontainer-use-staterefs) for operation logging and state persistence - Worktrees in
~/.config/container-use/worktrees/for filesystem representation
Key Principle: All operations MUST go through environment tools - NEVER use git commands directly in environments.
- Use ONLY environment tools for ANY and ALL file, code, or shell operations - NO EXCEPTIONS
- DO NOT install or use git CLI with environment_run_cmd tool - environment tools handle git integration automatically
- ALWAYS inform user how to view your work:
cu log <env-id>andcu checkout <env-id> - Failure to inform user makes work inaccessible to others
# Build binary
go build -o container-use ./cmd/container-use
# Using Dagger
dagger call build --platform=current export --path ./container-use# Run all tests
go test ./...
# Unit tests only (fast, no containers)
go test -short ./...
# Integration tests
go test -count=1 -v ./environment
# Run single test
go test -run TestSpecificFunction ./path/to/package
go test -run TestSpecificFunction -v ./path/to/package
# Run tests in specific package
go test ./repository
go test ./environment# Format code (REQUIRED before committing)
go fmt ./...
# Lint
dagger call lint# Install dependencies
go mod download
# Clean up dependencies
go mod tidy- Order: stdlib imports first, then third-party imports
- Group blank lines between stdlib and third-party
- No unused imports (checked by linter)
- Example:
import ( "context" "fmt" "os" "dagger.io/dagger" "github.com/spf13/cobra" )
- Always wrap errors with context:
fmt.Errorf("description: %w", err) - Never suppress errors with empty catch blocks
- Use descriptive error messages that explain what failed
- Example:
if err != nil { return fmt.Errorf("failed to open repository: %w", err) }
- Use
log/slogfor structured logging - Context-aware logging with key-value pairs
- Log at appropriate levels (Info, Warn, Error, Debug)
- Example:
slog.Info("Creating environment", "id", env.ID, "workdir", workdir) slog.Error("Command failed", "command", cmd, "error", err)
- Accept
context.Contextas first parameter in functions that may block or need cancellation - Pass context through all call chains
- Use
context.Background()only when no context is available - Example:
func (r *Repository) Create(ctx context.Context, dag *dagger.Client, title string) (*Environment, error)
- Exported types: PascalCase (
Environment,Repository) - Exported functions/methods: PascalCase (
New(),Load(),Create()) - Unexported types/methods: camelCase (
repository,loadState) - Constants: UPPER_SNAKE_CASE for package-level, camelCase for local
- Interfaces: Simple names, often -er suffix when appropriate
- Interfaces:
Reader,Writer,LockManager
- Use struct fields with JSON tags for serialization
- Omit empty fields with
omitemptytag - Keep structs focused on single responsibility
- Example:
type EnvironmentConfig struct { Workdir string `json:"workdir,omitempty"` BaseImage string `json:"base_image,omitempty"` SetupCommands []string `json:"setup_commands,omitempty"` }
- Constructor functions:
New()for creating new instances - Getters: No "Get" prefix preferred (e.g.,
repo.Workdir()notrepo.GetWorkdir()) - Setters:
Set()prefix for methods that modify state - Boolean returns: Return bool, error (not just bool)
- Example:
func New(ctx context.Context, args NewEnvArgs) (*Environment, error) func (env *Environment) Workdir() string func (kv *KVList) Set(key, value string)
- Test file naming:
<package>_test.go(e.g.,repository_test.go) - Test function naming:
Test<FunctionName>orTest<FeatureName> - Table-driven tests: Use for multiple test cases
- Integration tests: In
integration/subdirectory - Test helpers: Create reusable helpers in
test_helpers.goorhelpers.go - Setup/teardown: Use
t.Parallel()for parallel tests,t.Cleanup()for teardown - Skip tests: Use
if testing.Short() { t.Skip("...") }for slow tests - Assertions: Use
require.NoError()for setup,assert.*for assertions
- Package-level constants: UPPER_SNAKE_CASE
- Local constants: camelCase
- Use
constblock for related constants - Example:
const ( containerUseRemote = "container-use" gitNotesLogRef = "container-use" gitNotesStateRef = "container-use-state" )
- Exported functions must have godoc comments
- Use present tense for function descriptions
- Explain "why" not "what" for complex logic
- Package comments should explain the package's purpose
- Example:
// New creates a new environment with the given configuration. // The environment's container is built from the base image with // setup commands applied. func New(ctx context.Context, args NewEnvArgs) (*Environment, error)
- Never use
as any,@ts-ignore, or@ts-expect-errorto suppress type errors - Always return typed errors, not generic
errorwhen specific error types exist - Use explicit type assertions with ok check:
val, ok := someInterface.(ConcreteType)
- Use
sync.Mutexfor protecting shared state - Use
sync.RWMutexfor read-heavy workloads - Use
context.Contextfor cancellation and timeouts - Avoid global mutable state
- NEVER run git commands manually in environments - use provided environment tools
- Environment operations handle git integration automatically
- All file/code operations must use environment tools
- Create:
environment_create→ generates unique ID (pet name) → creates git branch → initializes container → runs setup commands - Open:
environment_open→ loads existing environment by ID → returns environment info and services - Update: Use environment tools → changes committed to worktree → synced to git notes
- Delete: Environment branch deleted → worktree removed → git notes cleaned up
- Container creation:
dag.Container().From(image)- builds from base image - Directory mounting:
container.WithDirectory("/workdir", sourceDir)- mounts code - Service binding:
container.WithServiceBinding(name, service)- for multi-container environments - Port exposure:
container.WithExposedPort(port)- for background services - Exporting:
directory.Export(ctx, path, ExportOpts{Wipe: true})- writes to worktree - File operations:
directory.File(path),directory.Directory(path)- access files
- environment_create: Create new environment from current git state
- environment_open: Load existing environment
- environment_list: List all environments
- environment_config: Update base image, setup commands, env vars (rebuilds container)
- environment_run_cmd: Run commands (foreground or background with ports)
- environment_file_write / file_read / file_edit / file_delete: File operations
- environment_add_service: Add database/cache services
- environment_checkpoint: Export environment as container image
- Always validate repository is git-initialized before operations
- Use
repository.Open()orrepository.OpenWithBasePath()for repo access - Environment IDs are auto-generated pet names (e.g., "adjective-animal")
- Configuration lives in
.container-use/environment.json - Use
cu configcommands to manage environment defaults
cmd/container-use/ # CLI entry points
environment/ # Core environment management
repository/ # Git operations and storage
mcpserver/ # MCP protocol implementation
examples/ # Usage examples
docs/ # Documentation (Mintlify)
- One struct per file where practical
- Group related functions together
- Place test files next to implementation files
- Keep integration tests in
integration/subdirectory
- Run
go fmt ./...to format code - Run
go test ./...to verify all tests pass - Run
go mod tidyto clean up dependencies - Run
dagger call lintto check for linting issues - Ensure all error paths are tested
- Add tests for new features
- Update documentation if public API changes