compose

package
v0.3.9 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: MIT Imports: 32 Imported by: 1

Documentation

Overview

Package compose provides a small API to execute commands in Compose services via Docker Engine.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Down added in v0.2.0

func Down(ctx context.Context, projectName string) error

Down cleans up all resources (containers and networks) associated with the project. It ignores "not found" errors for idempotency.

Types

type Cmd

type Cmd struct {
	// Public fields
	Service types.ServiceConfig
	Args    []string
	Env     []string
	// WorkingDir overrides the docker-compose.yml working_dir for this Cmd.
	// Leave empty to use the service config or image default.
	WorkingDir string

	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer
	// contains filtered or unexported fields
}

Cmd represents a pending command execution, similar to os/exec.Cmd.

func Command added in v0.3.0

func Command(service string, arg ...string) *Cmd

Command returns a Cmd to execute the given args in the named service. It loads the compose project from the current working directory.

Note: Each call loads project configuration. For repeated invocations, use LoadProject once and reuse Project.Command().

func CommandContext added in v0.3.0

func CommandContext(ctx context.Context, service string, arg ...string) *Cmd

CommandContext returns a Cmd to execute the given args in the named service, bound to the provided context for lifecycle cancellation.

Note: Each call loads project configuration. For repeated invocations, use LoadProject once and reuse Project.CommandContext().

func (*Cmd) CombinedOutput

func (c *Cmd) CombinedOutput() ([]byte, error)

CombinedOutput runs the command and returns its combined standard output and standard error. If created via CommandContext, its context controls cancellation.

func (*Cmd) Environ added in v0.3.0

func (c *Cmd) Environ() []string

Environ returns a copy of the environment in which the command would run.

func (*Cmd) Output

func (c *Cmd) Output() ([]byte, error)

Output runs the command and returns its standard output. If created via CommandContext, its context controls cancellation.

func (*Cmd) Run

func (c *Cmd) Run() error

Run starts the container and waits for it to exit, similar to (*exec.Cmd).Run. If created via CommandContext, its context controls cancellation.

func (*Cmd) Start

func (c *Cmd) Start() (startErr error)

Start creates and starts the container for the configured service command.

func (*Cmd) StderrPipe added in v0.3.0

func (c *Cmd) StderrPipe() (io.ReadCloser, error)

StderrPipe returns a pipe that will be connected to the command's standard error.

It is an error to call StderrPipe after the command has started or when Stderr is already set.

func (*Cmd) StdinPipe added in v0.3.0

func (c *Cmd) StdinPipe() (io.WriteCloser, error)

StdinPipe returns a pipe that will be connected to the command's standard input.

It is an error to call StdinPipe after the command has started or when Stdin is already set.

func (*Cmd) StdoutPipe added in v0.3.0

func (c *Cmd) StdoutPipe() (io.ReadCloser, error)

StdoutPipe returns a pipe that will be connected to the command's standard output.

It is an error to call StdoutPipe after the command has started or when Stdout is already set.

func (*Cmd) String added in v0.3.0

func (c *Cmd) String() string

String returns a human-friendly representation of the command.

When Args is empty, it returns "<default>" to indicate that Docker Engine/image defaults (or YAML service.command via resolution) will be used.

func (*Cmd) Wait

func (c *Cmd) Wait() error

Wait waits for the started container to exit and returns its exit status. If created via CommandContext, its context controls cancellation.

func (*Cmd) WaitUntilHealthy added in v0.2.0

func (c *Cmd) WaitUntilHealthy() error

WaitUntilHealthy blocks until the started container becomes healthy. If created via CommandContext, its context controls cancellation.

Strict behavior: - If the service has no healthcheck defined, it returns an error immediately. - If the container becomes unhealthy or stops running, it returns an error immediately.

type ExitError

type ExitError struct {
	// Code is the exit status from the wait response.
	Code int
	// Stderr is a snapshot of standard error when captured by Output.
	Stderr []byte
	// ContainerState is the last known container state from Docker inspect.
	// It is nil if inspect fails.
	ContainerState *container.State
}

ExitError is returned when a container exits with a non-zero status. It is analogous to os/exec.ExitError (ContainerState mirrors ProcessState).

func (*ExitError) Error

func (e *ExitError) Error() string

func (*ExitError) ExitCode

func (e *ExitError) ExitCode() int

ExitCode returns the process exit status code.

func (*ExitError) Pid added in v0.3.0

func (e *ExitError) Pid() int

Pid returns the container's process ID, or 0 if unavailable.

type Project added in v0.3.0

type Project types.Project

Project is a compose-go project with helper methods for compose-exec.

func LoadProject

func LoadProject(ctx context.Context, dir string, files ...string) (*Project, error)

LoadProject loads a compose project from compose files within dir.

If files is empty, it defaults to docker-compose.yml and docker-compose.override.yml (the latter only if it exists).

Environment variable resolution follows compose-go behavior, including .env in dir.

func (*Project) Command added in v0.3.0

func (p *Project) Command(service string, arg ...string) *Cmd

Command returns a Cmd to execute args in the named service.

func (*Project) CommandContext added in v0.3.0

func (p *Project) CommandContext(ctx context.Context, service string, arg ...string) *Cmd

CommandContext returns a Cmd bound to ctx to execute args in the named service.

func (*Project) Service added in v0.3.0

func (p *Project) Service(name string) (*Service, error)

Service returns a Service bound to the named compose service.

type Service

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

Service is an execution context bound to a Compose service definition.

It is intentionally small; lifecycle is managed per Cmd.

func (*Service) Command

func (s *Service) Command(arg ...string) *Cmd

Command returns a Cmd to execute the given command arguments in the service.

When called with zero args, Docker Engine/image defaults (or YAML service.command via command resolution) will be used. Use CommandContext to bind a context.

func (*Service) CommandContext added in v0.3.0

func (s *Service) CommandContext(ctx context.Context, arg ...string) *Cmd

CommandContext returns a Cmd to execute the given command arguments in the service, bound to the provided context for lifecycle cancellation.

It panics if ctx is nil, matching os/exec.CommandContext behavior.

Jump to

Keyboard shortcuts

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