Documentation
¶
Overview ¶
Package compose provides a small API to execute commands in Compose services via Docker Engine.
Index ¶
- func Down(ctx context.Context, projectName string) error
- type Cmd
- func (c *Cmd) CombinedOutput() ([]byte, error)
- func (c *Cmd) Environ() []string
- func (c *Cmd) Output() ([]byte, error)
- func (c *Cmd) Run() error
- func (c *Cmd) Start() (startErr error)
- func (c *Cmd) StderrPipe() (io.ReadCloser, error)
- func (c *Cmd) StdinPipe() (io.WriteCloser, error)
- func (c *Cmd) StdoutPipe() (io.ReadCloser, error)
- func (c *Cmd) String() string
- func (c *Cmd) Wait() error
- func (c *Cmd) WaitUntilHealthy() error
- type ExitError
- type Project
- type Service
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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
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
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 ¶
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
Environ returns a copy of the environment in which the command would run.
func (*Cmd) Output ¶
Output runs the command and returns its standard output. If created via CommandContext, its context controls cancellation.
func (*Cmd) Run ¶
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) 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
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 ¶
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
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).
type Project ¶ added in v0.3.0
Project is a compose-go project with helper methods for compose-exec.
func LoadProject ¶
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
Command returns a Cmd to execute args in the named service.
func (*Project) CommandContext ¶ added in v0.3.0
CommandContext returns a Cmd bound to ctx to execute args in the named 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 ¶
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
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.