dockercompose

package
v0.0.0-...-9dee9fb Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package dockercompose implements a platform.Provider that maps abstract capability declarations to Docker Compose services, networks, and volumes. It uses only the standard library and invokes docker compose via exec.Command.

Index

Constants

View Source
const (
	CapContainerRuntime  = "container_runtime"
	CapDatabase          = "database"
	CapMessageQueue      = "message_queue"
	CapNetwork           = "network"
	CapKubernetesCluster = "kubernetes_cluster"
	CapLoadBalancer      = "load_balancer"
	CapNamespace         = "namespace"
	CapPersistentVolume  = "persistent_volume"
)

Capability type constants used for mapping abstract declarations to compose resources.

View Source
const (
	// ProviderName is the identifier for the Docker Compose provider.
	ProviderName = "docker-compose"

	// ProviderVersion is the current version of the Docker Compose provider.
	ProviderVersion = "0.1.0"
)

Variables

This section is empty.

Functions

func NewProvider

func NewProvider() platform.Provider

NewProvider creates a new DockerComposeProvider. This is the ProviderFactory function registered with the engine.

Types

type ComposeCapabilityMapper

type ComposeCapabilityMapper struct{}

ComposeCapabilityMapper maps abstract capability declarations to Docker Compose resource plans. It understands how to convert capability-level abstractions into the concrete services, networks, and volumes that Docker Compose manages.

func NewCapabilityMapper

func NewCapabilityMapper() *ComposeCapabilityMapper

NewCapabilityMapper creates a new ComposeCapabilityMapper.

func (*ComposeCapabilityMapper) CanMap

func (m *ComposeCapabilityMapper) CanMap(capabilityType string) bool

CanMap returns true if this mapper can handle the given capability type.

func (*ComposeCapabilityMapper) Map

Map translates a capability declaration into one or more resource plans.

func (*ComposeCapabilityMapper) ValidateConstraints

ValidateConstraints checks if a capability declaration satisfies all constraints.

type ComposeExecutor

type ComposeExecutor interface {
	// Up starts services defined in the compose file.
	Up(ctx context.Context, projectDir string, files ...string) (string, error)

	// Down stops and removes services defined in the compose file.
	Down(ctx context.Context, projectDir string, files ...string) (string, error)

	// Ps lists running containers for the compose project.
	Ps(ctx context.Context, projectDir string, files ...string) (string, error)

	// Logs retrieves logs from compose services.
	Logs(ctx context.Context, projectDir string, service string, files ...string) (string, error)

	// Version returns the docker compose version string.
	Version(ctx context.Context) (string, error)

	// IsAvailable checks whether docker compose is installed and reachable.
	IsAvailable(ctx context.Context) error
}

ComposeExecutor defines the interface for running docker compose commands. This abstraction allows tests to inject a mock executor.

type ComposeFile

type ComposeFile struct {
	// Services maps service names to their definitions.
	Services map[string]*ComposeService `yaml:"services" json:"services"`

	// Networks maps network names to their definitions.
	Networks map[string]*ComposeNetwork `yaml:"networks,omitempty" json:"networks,omitempty"`

	// Volumes maps volume names to their definitions.
	Volumes map[string]*ComposeVolume `yaml:"volumes,omitempty" json:"volumes,omitempty"`
}

ComposeFile represents the top-level structure of a docker-compose.yml file.

func NewComposeFile

func NewComposeFile() *ComposeFile

NewComposeFile creates an empty ComposeFile with initialized maps.

func (*ComposeFile) AddNetwork

func (cf *ComposeFile) AddNetwork(name string, net *ComposeNetwork)

AddNetwork adds a network to the compose file.

func (*ComposeFile) AddService

func (cf *ComposeFile) AddService(name string, svc *ComposeService)

AddService adds a service to the compose file.

func (*ComposeFile) AddVolume

func (cf *ComposeFile) AddVolume(name string, vol *ComposeVolume)

AddVolume adds a volume to the compose file.

func (*ComposeFile) MarshalYAML

func (cf *ComposeFile) MarshalYAML() (string, error)

MarshalYAML produces a valid docker-compose.yml representation as a string. It uses manual marshaling to control output order and avoid external YAML dependencies (stdlib only).

type ComposeNetwork

type ComposeNetwork struct {
	// Driver is the network driver (e.g., "bridge", "overlay").
	Driver string `yaml:"driver,omitempty" json:"driver,omitempty"`

	// External indicates whether the network is externally managed.
	External bool `yaml:"external,omitempty" json:"external,omitempty"`

	// Labels are metadata key-value pairs.
	Labels map[string]string `yaml:"labels,omitempty" json:"labels,omitempty"`

	// DriverOpts holds driver-specific options.
	DriverOpts map[string]string `yaml:"driver_opts,omitempty" json:"driver_opts,omitempty"`

	// IPAM configures IP address management.
	IPAM *IPAMConfig `yaml:"ipam,omitempty" json:"ipam,omitempty"`
}

ComposeNetwork represents a Docker Compose network definition.

type ComposeService

type ComposeService struct {
	// Image is the Docker image to use.
	Image string `yaml:"image" json:"image"`

	// ContainerName is an explicit container name.
	ContainerName string `yaml:"container_name,omitempty" json:"container_name,omitempty"`

	// Ports maps host ports to container ports.
	Ports []string `yaml:"ports,omitempty" json:"ports,omitempty"`

	// Environment holds environment variable definitions.
	Environment map[string]string `yaml:"environment,omitempty" json:"environment,omitempty"`

	// Volumes are volume mounts for the service.
	Volumes []string `yaml:"volumes,omitempty" json:"volumes,omitempty"`

	// Networks lists the networks this service is attached to.
	Networks []string `yaml:"networks,omitempty" json:"networks,omitempty"`

	// DependsOn lists services that must start before this one.
	DependsOn []string `yaml:"depends_on,omitempty" json:"depends_on,omitempty"`

	// Deploy holds deployment configuration like replicas and resource limits.
	Deploy *DeployConfig `yaml:"deploy,omitempty" json:"deploy,omitempty"`

	// Healthcheck defines a container health check.
	Healthcheck *HealthcheckConfig `yaml:"healthcheck,omitempty" json:"healthcheck,omitempty"`

	// Restart is the restart policy.
	Restart string `yaml:"restart,omitempty" json:"restart,omitempty"`

	// Command overrides the default container command.
	Command string `yaml:"command,omitempty" json:"command,omitempty"`

	// Labels are metadata key-value pairs applied to the container.
	Labels map[string]string `yaml:"labels,omitempty" json:"labels,omitempty"`
}

ComposeService represents a single service in docker-compose.yml.

type ComposeVolume

type ComposeVolume struct {
	// Driver is the volume driver.
	Driver string `yaml:"driver,omitempty" json:"driver,omitempty"`

	// External indicates whether the volume is externally managed.
	External bool `yaml:"external,omitempty" json:"external,omitempty"`

	// Labels are metadata key-value pairs.
	Labels map[string]string `yaml:"labels,omitempty" json:"labels,omitempty"`

	// DriverOpts holds driver-specific options.
	DriverOpts map[string]string `yaml:"driver_opts,omitempty" json:"driver_opts,omitempty"`
}

ComposeVolume represents a Docker Compose volume definition.

type DeployConfig

type DeployConfig struct {
	// Replicas is the number of container instances.
	Replicas int `yaml:"replicas,omitempty" json:"replicas,omitempty"`

	// Resources defines resource limits and reservations.
	Resources *ResourcesConfig `yaml:"resources,omitempty" json:"resources,omitempty"`
}

DeployConfig holds deployment-related configuration.

type DockerComposeProvider

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

DockerComposeProvider implements platform.Provider by mapping abstract capabilities to Docker Compose services, networks, and volumes. It executes docker compose commands via exec.Command and persists state to the local filesystem. No Docker SDK dependency is required.

func NewProviderWithExecutor

func NewProviderWithExecutor(exec ComposeExecutor) *DockerComposeProvider

NewProviderWithExecutor creates a DockerComposeProvider with a custom executor, enabling tests to inject a mock.

func (*DockerComposeProvider) Capabilities

func (p *DockerComposeProvider) Capabilities() []platform.CapabilityType

Capabilities returns the set of capability types this provider supports.

func (*DockerComposeProvider) Close

func (p *DockerComposeProvider) Close() error

Close releases any resources held by the provider.

func (*DockerComposeProvider) ComposeFilePath

func (p *DockerComposeProvider) ComposeFilePath() string

ComposeFilePath returns the path to the generated docker-compose.yml.

func (*DockerComposeProvider) CredentialBroker

func (p *DockerComposeProvider) CredentialBroker() platform.CredentialBroker

CredentialBroker returns nil because Docker Compose does not support credential brokering.

func (*DockerComposeProvider) Down

Down runs docker compose down for the project.

func (*DockerComposeProvider) FidelityReports

FidelityReports returns fidelity gap reports for capabilities that are not fully implemented by Docker Compose.

func (*DockerComposeProvider) GenerateComposeFile

func (p *DockerComposeProvider) GenerateComposeFile(plans []platform.ResourcePlan) (*ComposeFile, error)

GenerateComposeFile builds a ComposeFile from a set of resource plans and writes it to the project directory.

func (*DockerComposeProvider) Healthy

func (p *DockerComposeProvider) Healthy(ctx context.Context) error

Healthy returns nil if Docker is reachable.

func (*DockerComposeProvider) Initialize

func (p *DockerComposeProvider) Initialize(ctx context.Context, config map[string]any) error

Initialize validates that Docker is available and sets up the project directory. Config keys:

  • project_dir: directory for docker-compose.yml (default: current directory)
  • state_dir: directory for state files (default: project_dir/.platform-state)

func (*DockerComposeProvider) MapCapability

MapCapability resolves an abstract capability declaration to provider-specific resource plans using the capability mapper.

func (*DockerComposeProvider) Name

func (p *DockerComposeProvider) Name() string

Name returns the provider identifier.

func (*DockerComposeProvider) ResourceDriver

func (p *DockerComposeProvider) ResourceDriver(resourceType string) (platform.ResourceDriver, error)

ResourceDriver returns the driver for a specific resource type.

func (*DockerComposeProvider) StateStore

func (p *DockerComposeProvider) StateStore() platform.StateStore

StateStore returns the file-system-based state store.

func (*DockerComposeProvider) Up

Up runs docker compose up for the project.

func (*DockerComposeProvider) Version

func (p *DockerComposeProvider) Version() string

Version returns the provider version string.

func (*DockerComposeProvider) WriteComposeFile

func (p *DockerComposeProvider) WriteComposeFile(cf *ComposeFile) error

WriteComposeFile writes the compose file to the project directory.

type FileStateStore

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

FileStateStore persists platform state as JSON files in a local directory. It implements platform.StateStore for local Docker Compose development.

func NewFileStateStore

func NewFileStateStore(baseDir string) (*FileStateStore, error)

NewFileStateStore creates a FileStateStore rooted at baseDir. The directory is created if it does not exist.

func (*FileStateStore) AddDependency

func (s *FileStateStore) AddDependency(_ context.Context, dep platform.DependencyRef) error

AddDependency records a cross-resource dependency.

func (*FileStateStore) DeleteResource

func (s *FileStateStore) DeleteResource(_ context.Context, contextPath, resourceName string) error

DeleteResource removes a resource from state.

func (*FileStateStore) Dependencies

func (s *FileStateStore) Dependencies(_ context.Context, contextPath, _ string) ([]platform.DependencyRef, error)

Dependencies returns dependency references for resources that depend on the given resource.

func (*FileStateStore) GetPlan

func (s *FileStateStore) GetPlan(_ context.Context, planID string) (*platform.Plan, error)

GetPlan retrieves an execution plan by ID.

func (*FileStateStore) GetResource

func (s *FileStateStore) GetResource(_ context.Context, contextPath, resourceName string) (*platform.ResourceOutput, error)

GetResource retrieves a resource's state from the state directory.

func (*FileStateStore) ListPlans

func (s *FileStateStore) ListPlans(_ context.Context, _ string, limit int) ([]*platform.Plan, error)

ListPlans lists plans for a context path, ordered by creation time descending.

func (*FileStateStore) ListResources

func (s *FileStateStore) ListResources(_ context.Context, contextPath string) ([]*platform.ResourceOutput, error)

ListResources returns all resources in a context path.

func (*FileStateStore) Lock

func (s *FileStateStore) Lock(_ context.Context, contextPath string, ttl time.Duration) (platform.LockHandle, error)

Lock acquires an advisory lock for a context path.

func (*FileStateStore) SavePlan

func (s *FileStateStore) SavePlan(_ context.Context, plan *platform.Plan) error

SavePlan persists an execution plan.

func (*FileStateStore) SaveResource

func (s *FileStateStore) SaveResource(_ context.Context, contextPath string, output *platform.ResourceOutput) error

SaveResource persists a resource output to the state directory.

type HealthcheckConfig

type HealthcheckConfig struct {
	// Test is the health check command.
	Test []string `yaml:"test" json:"test"`

	// Interval is the time between health checks.
	Interval string `yaml:"interval,omitempty" json:"interval,omitempty"`

	// Timeout is the maximum time for a single check.
	Timeout string `yaml:"timeout,omitempty" json:"timeout,omitempty"`

	// Retries is the number of consecutive failures before unhealthy.
	Retries int `yaml:"retries,omitempty" json:"retries,omitempty"`

	// StartPeriod is the time to wait before starting checks.
	StartPeriod string `yaml:"start_period,omitempty" json:"start_period,omitempty"`
}

HealthcheckConfig defines the container health check.

type IPAMConfig

type IPAMConfig struct {
	// Driver is the IPAM driver.
	Driver string `yaml:"driver,omitempty" json:"driver,omitempty"`

	// Config holds per-subnet configuration.
	Config []IPAMPoolConfig `yaml:"config,omitempty" json:"config,omitempty"`
}

IPAMConfig holds IP address management configuration.

type IPAMPoolConfig

type IPAMPoolConfig struct {
	// Subnet is the subnet CIDR.
	Subnet string `yaml:"subnet,omitempty" json:"subnet,omitempty"`
}

IPAMPoolConfig defines a single IPAM subnet pool.

type MockCall

type MockCall struct {
	Method string
	Args   []string
}

MockCall records a single executor method invocation.

type MockExecutor

type MockExecutor struct {
	// UpFn is called when Up is invoked. If nil, returns empty string and no error.
	UpFn func(ctx context.Context, projectDir string, files ...string) (string, error)

	// DownFn is called when Down is invoked.
	DownFn func(ctx context.Context, projectDir string, files ...string) (string, error)

	// PsFn is called when Ps is invoked.
	PsFn func(ctx context.Context, projectDir string, files ...string) (string, error)

	// LogsFn is called when Logs is invoked.
	LogsFn func(ctx context.Context, projectDir string, service string, files ...string) (string, error)

	// VersionFn is called when Version is invoked.
	VersionFn func(ctx context.Context) (string, error)

	// IsAvailableFn is called when IsAvailable is invoked.
	IsAvailableFn func(ctx context.Context) error

	// Calls records all method calls for assertion purposes.
	Calls []MockCall
}

MockExecutor is a test double for ComposeExecutor that records calls and returns pre-configured responses.

func (*MockExecutor) Down

func (m *MockExecutor) Down(ctx context.Context, projectDir string, files ...string) (string, error)

Down implements ComposeExecutor.

func (*MockExecutor) IsAvailable

func (m *MockExecutor) IsAvailable(ctx context.Context) error

IsAvailable implements ComposeExecutor.

func (*MockExecutor) Logs

func (m *MockExecutor) Logs(ctx context.Context, projectDir string, service string, files ...string) (string, error)

Logs implements ComposeExecutor.

func (*MockExecutor) Ps

func (m *MockExecutor) Ps(ctx context.Context, projectDir string, files ...string) (string, error)

Ps implements ComposeExecutor.

func (*MockExecutor) Up

func (m *MockExecutor) Up(ctx context.Context, projectDir string, files ...string) (string, error)

Up implements ComposeExecutor.

func (*MockExecutor) Version

func (m *MockExecutor) Version(ctx context.Context) (string, error)

Version implements ComposeExecutor.

type ResourceSpec

type ResourceSpec struct {
	// CPUs is the CPU limit (e.g., "0.5", "2").
	CPUs string `yaml:"cpus,omitempty" json:"cpus,omitempty"`

	// Memory is the memory limit (e.g., "512M", "1G").
	Memory string `yaml:"memory,omitempty" json:"memory,omitempty"`
}

ResourceSpec defines CPU and memory resource values.

type ResourcesConfig

type ResourcesConfig struct {
	// Limits are the maximum resource constraints.
	Limits *ResourceSpec `yaml:"limits,omitempty" json:"limits,omitempty"`

	// Reservations are the minimum guaranteed resources.
	Reservations *ResourceSpec `yaml:"reservations,omitempty" json:"reservations,omitempty"`
}

ResourcesConfig holds resource limits and reservations.

type ShellExecutor

type ShellExecutor struct {
	// ComposeCommand is the base command to use. Defaults to "docker" with "compose" subcommand.
	ComposeCommand string
}

ShellExecutor executes docker compose commands through the system shell.

func NewShellExecutor

func NewShellExecutor() *ShellExecutor

NewShellExecutor creates a ShellExecutor that uses the system docker compose.

func (*ShellExecutor) Down

func (e *ShellExecutor) Down(ctx context.Context, projectDir string, files ...string) (string, error)

Down stops and removes services with docker compose down.

func (*ShellExecutor) IsAvailable

func (e *ShellExecutor) IsAvailable(ctx context.Context) error

IsAvailable checks whether docker compose is installed and reachable.

func (*ShellExecutor) Logs

func (e *ShellExecutor) Logs(ctx context.Context, projectDir string, service string, files ...string) (string, error)

Logs retrieves logs for a service with docker compose logs.

func (*ShellExecutor) Ps

func (e *ShellExecutor) Ps(ctx context.Context, projectDir string, files ...string) (string, error)

Ps lists containers with docker compose ps.

func (*ShellExecutor) Up

func (e *ShellExecutor) Up(ctx context.Context, projectDir string, files ...string) (string, error)

Up starts services with docker compose up -d.

func (*ShellExecutor) Version

func (e *ShellExecutor) Version(ctx context.Context) (string, error)

Version returns the docker compose version.

Directories

Path Synopsis
Package drivers provides resource driver implementations for the Docker Compose provider.
Package drivers provides resource driver implementations for the Docker Compose provider.

Jump to

Keyboard shortcuts

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