Documentation
¶
Overview ¶
Package compose loads Compose files — YAML, JSON or HCL2 — into the Compose Specification's application model, and runs that model on a go-wasmcontainers engine through its API.
Loading follows the specification (compose-spec/compose-spec): file discovery and override files, interpolation per file, merge rules (!reset, !override, unique keys), extends, include, profiles. Every input is bounded: a Compose file is untrusted.
Index ¶
- Constants
- Variables
- func ConfigHash(p *Project, s *Service) (string, error)
- func Marshal(p *Project, f Format) ([]byte, error)
- func NormalizeProjectName(s string) string
- type Build
- type BuildFunc
- type Command
- type Container
- type Dependency
- type Format
- type Healthcheck
- type Mount
- type Network
- type Options
- type Port
- type Project
- type RestartPolicy
- type Runner
- func (r *Runner) Attach(ctx context.Context, p *Project, services []string) error
- func (r *Runner) BuildServices(ctx context.Context, p *Project, services []string, noCache bool) error
- func (r *Runner) Containers(ctx context.Context, project string) ([]Container, error)
- func (r *Runner) Down(ctx context.Context, p *Project, volumes bool) error
- func (r *Runner) Logs(ctx context.Context, p *Project, services []string, follow bool) error
- func (r *Runner) Pull(ctx context.Context, p *Project, services []string) error
- func (r *Runner) Restart(ctx context.Context, p *Project, services []string) error
- func (r *Runner) Rm(ctx context.Context, p *Project, services []string, stop bool) error
- func (r *Runner) Start(ctx context.Context, p *Project, services []string) error
- func (r *Runner) Stop(ctx context.Context, p *Project, services []string) error
- func (r *Runner) Up(ctx context.Context, p *Project, o UpOptions) error
- type Service
- type ServiceNetwork
- type UpOptions
- type Volume
Constants ¶
const ( MaxServices = 1000 MaxReplicas = 64 MaxContainers = 256 // the engine's default --max-containers )
Bounds of a project, beyond those of one file.
const ( LabelProject = "com.docker.compose.project" LabelService = "com.docker.compose.service" LabelContainerNumber = "com.docker.compose.container-number" LabelConfigHash = "com.docker.compose.config-hash" LabelImage = "com.docker.compose.image" LabelOneoff = "com.docker.compose.oneoff" LabelNetwork = "com.docker.compose.network" LabelVolume = "com.docker.compose.volume" LabelDependsOn = "com.docker.compose.depends_on" )
The labels a project's resources carry. The specification mandates the first two and reserves the prefix (02-model.md, container_spec.md); the others are the reference implementation's names (docs/REFERENCES.md).
const ( ServiceStarted = "service_started" ServiceHealthy = "service_healthy" ServiceCompletedSuccessfully = "service_completed_successfully" )
Dependency conditions (workload_spec.md, depends_on long syntax).
const DefaultHealthTimeout = 5 * time.Minute
DefaultHealthTimeout bounds the wait for one dependency to become healthy (UpOptions.HealthTimeout overrides it).
const ReservedLabelPrefix = "com.docker.compose"
ReservedLabelPrefix is the label prefix the specification reserves for the implementation (container_spec.md, labels).
Variables ¶
var DefaultFileNames = []string{
"compose.yaml", "compose.yml", "compose.json", "compose.hcl",
"docker-compose.yaml", "docker-compose.yml",
}
DefaultFileNames are tried in order when no file is named: the specification's canonical names, ours, then its legacy ones.
var DefaultOverrideNames = []string{
"compose.override.yaml", "compose.override.yml", "compose.override.json", "compose.override.hcl",
"docker-compose.override.yaml", "docker-compose.override.yml",
}
DefaultOverrideNames are merged after the discovered file.
var ErrNoBuilder = errors.New("this service must be built, and no builder is configured")
ErrNoBuilder is returned when a build is needed and the Runner has no BuildFunc.
var ErrUnhealthy = errors.New("unhealthy")
ErrUnhealthy is a dependency whose health check reported unhealthy, or that stopped before it was healthy.
Functions ¶
func ConfigHash ¶
ConfigHash is a digest of everything that makes a service's container: a container whose label differs is recreated by up.
func Marshal ¶
Marshal writes the normalised model in a format; loading the result gives the same model back (the round trip is tested for all three).
func NormalizeProjectName ¶
NormalizeProjectName makes a directory name a project name: lowercase, only [a-z0-9_-], no leading '-' or '_' (as the reference loader does).
Types ¶
type Build ¶
type Build struct {
// Context is the build context's directory in the project's file
// system (resolved from the Compose file's directory).
Context string `json:"context"`
// Dockerfile is relative to Context (default Containerfile, else
// Dockerfile); DockerfileInline is the file's content instead.
Dockerfile string `json:"dockerfile,omitempty"`
DockerfileInline string `json:"dockerfile_inline,omitempty"`
Args map[string]*string `json:"args,omitempty"`
Target string `json:"target,omitempty"`
// Tags name the built image besides the service's image name.
Tags []string `json:"tags,omitempty"`
NoCache bool `json:"no_cache,omitempty"`
}
Build is a service's build section (build.md), built by go-wasmcontainers/build through the engine.
type BuildFunc ¶ added in v0.2.0
BuildFunc builds a service's image (tagged with p.ImageName(s) and its build tags) and returns its ID. Package imagebuild provides one, over go-wasmcontainers/build; the loader itself does not link a builder.
type Command ¶
type Command []string
Command is a command line: nil is "the image's", an empty non-nil one clears the image's.
type Container ¶
type Container struct {
api.ContainerSummary
Name string
Service string
Number int
}
Container is a project's container as the engine lists it.
type Dependency ¶
type Dependency struct {
Condition string `json:"condition"`
Restart bool `json:"restart,omitempty"`
Required bool `json:"required"`
}
Dependency is one depends_on entry.
type Healthcheck ¶
type Healthcheck struct {
Test []string `json:"test,omitempty"`
Interval *time.Duration `json:"interval,omitempty"`
Timeout *time.Duration `json:"timeout,omitempty"`
StartPeriod *time.Duration `json:"start_period,omitempty"`
StartInterval *time.Duration `json:"start_interval,omitempty"`
Retries *int64 `json:"retries,omitempty"`
Disable bool `json:"disable,omitempty"`
}
Healthcheck is a service's health check.
type Mount ¶
type Mount struct {
Type string `json:"type"`
Source string `json:"source"`
Target string `json:"target"`
ReadOnly bool `json:"read_only,omitempty"`
}
Mount is a volume mounted into a service's containers. Only named volumes exist on this engine (Type "volume").
type Network ¶
type Network struct {
// Name is the network's name in the engine: "<project>_<key>" unless
// set, the key itself when external.
Name string `json:"name"`
External bool `json:"external,omitempty"`
Driver string `json:"driver,omitempty"`
Internal bool `json:"internal,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Extensions map[string]any `json:"-"`
}
Network is a top-level network.
type Options ¶
type Options struct {
// FS holds the project; every path below is slash-separated and
// relative to it, and nothing outside it is ever read. Natively, the
// CLI roots it at the project directory (os.OpenRoot, so symbolic
// links cannot leave it either); in a browser it is the page's files.
FS fs.FS
// WorkingDir is the project directory in FS (default ".").
WorkingDir string
// Files are the Compose files (-f), in merge order. Empty: discover
// compose.yaml (or .yml, .json, .hcl, or a legacy name) and its
// compose.override.* in WorkingDir.
Files []string
// ProjectName overrides every other source of the name (-p).
ProjectName string
// DefaultName is the name when no other source gives one (default:
// WorkingDir's base name, normalised).
DefaultName string
// Environment is the explicit environment interpolation reads, over
// the env files (natively the CLI passes its process environment; a
// browser has none).
Environment map[string]string
// EnvFiles replace the default ".env" of WorkingDir (--env-file).
EnvFiles []string
// Profiles are the active profiles (with COMPOSE_PROFILES).
Profiles []string
// Limits bound each file read; zero fields take tree.DefaultLimits.
Limits tree.Limits
}
Options say what to load.
type Port ¶ added in v0.4.0
type Port struct {
// Target is the container port or range ("80", "80-81").
Target string `json:"target"`
// Published is the host port or range; empty for one the system
// picks.
Published string `json:"published,omitempty"`
// HostIP is the host address; empty is the engine's default,
// 127.0.0.1 (the specification says 0.0.0.0: see the README).
HostIP string `json:"host_ip,omitempty"`
// Protocol is "tcp" (default) or "udp".
Protocol string `json:"protocol,omitempty"`
Name string `json:"name,omitempty"`
AppProtocol string `json:"app_protocol,omitempty"`
Mode string `json:"mode,omitempty"`
}
Port is a published port (workload_spec.md, ports, long syntax).
type Project ¶
type Project struct {
// Name is the project name: lowercase letters, digits, '-' and '_'.
Name string `json:"name"`
// WorkingDir is the project directory (in Options.FS).
WorkingDir string `json:"-"`
// Files are the Compose files read, in order.
Files []string `json:"-"`
// Services are the enabled services; DisabledServices those whose
// profiles are not active.
Services map[string]*Service `json:"services"`
DisabledServices map[string]*Service `json:"-"`
Networks map[string]*Network `json:"networks,omitempty"`
Volumes map[string]*Volume `json:"volumes,omitempty"`
// Extensions are the top-level `x-` keys.
Extensions map[string]any `json:"-"`
// Environment is what interpolation read (explicit environment over
// .env), with COMPOSE_PROJECT_NAME.
Environment map[string]string `json:"-"`
// Warnings are what was accepted but is not applied (unsupported
// attributes, unset variables).
Warnings []string `json:"-"`
}
Project is a loaded, merged and validated Compose application model.
func (*Project) ContainerName ¶
ContainerName is the name of a service's container number n.
func (*Project) EnableServices ¶
EnableServices activates services named on a command line, with their profiles (Compose Specification, profiles: an explicitly targeted service is enabled), and the dependencies they pull in.
func (*Project) ImageName ¶ added in v0.2.0
ImageName is the image a service's containers run: its `image`, else "<project>-<service>" for a service that is built (as the reference implementation names it; build.md lets an implementation compute one).
func (*Project) ServiceNames ¶
ServiceNames returns the enabled services' names, sorted.
type RestartPolicy ¶ added in v0.5.0
type RestartPolicy struct {
Condition string `json:"condition"`
MaxAttempts *int64 `json:"max_attempts,omitempty"`
}
RestartPolicy is deploy.restart_policy (deploy.md): condition none, on-failure or any (default), max_attempts (absent: never give up).
type Runner ¶
type Runner struct {
Client *api.Client
// Build builds a service's image (nil: services that need building
// fail with ErrNoBuilder).
Build BuildFunc
// Out receives progress and logs; Err warnings.
Out, Err io.Writer
}
Runner drives an engine through its API, as the compose CLI drives a daemon: it never reaches the engine any other way.
func (*Runner) Attach ¶
Attach follows the logs of the project's containers until they have all exited, or ctx is done (then they are stopped): `up` without -d.
func (*Runner) BuildServices ¶ added in v0.2.0
func (r *Runner) BuildServices(ctx context.Context, p *Project, services []string, noCache bool) error
BuildServices builds the services that have a build section (all of them when none is named): `compose build`.
func (*Runner) Containers ¶
Containers lists the project's containers (all states), sorted by service then number; the engine filters them by the project's label.
func (*Runner) Down ¶
Down stops and removes the project's containers and networks, and with volumes its volumes (never external ones).
func (*Runner) Logs ¶
Logs writes the containers' logs, each line prefixed by the container's name; with follow it returns when every stream has ended (each container exited) or ctx is done.
type Service ¶
type Service struct {
Name string `json:"-"`
Image string `json:"image,omitempty"`
Build *Build `json:"build,omitempty"`
Command Command `json:"command,omitempty"`
Entrypoint Command `json:"entrypoint,omitempty"`
Environment map[string]string `json:"environment,omitempty"`
WorkingDir string `json:"working_dir,omitempty"`
Volumes []Mount `json:"volumes,omitempty"`
Ports []Port `json:"ports,omitempty"`
// Expose are container ports reachable on the service's networks and
// not published ("80", "8000-8010/udp").
Expose []string `json:"expose,omitempty"`
Networks map[string]*ServiceNetwork `json:"networks,omitempty"`
NetworkMode string `json:"network_mode,omitempty"`
DependsOn map[string]Dependency `json:"depends_on,omitempty"`
Healthcheck *Healthcheck `json:"healthcheck,omitempty"`
Restart string `json:"restart,omitempty"`
// RestartPolicy is deploy.restart_policy; it wins over Restart
// (deploy.md: restart applies when restart_policy is not set).
RestartPolicy *RestartPolicy `json:"restart_policy,omitempty"`
Replicas int `json:"replicas"`
Labels map[string]string `json:"labels,omitempty"`
Profiles []string `json:"profiles,omitempty"`
ContainerName string `json:"container_name,omitempty"`
Extensions map[string]any `json:"-"`
}
Service is one service.
func (*Service) EngineRestart ¶ added in v0.5.0
EngineRestart is the engine's restart policy for the service: deploy.restart_policy when set, else restart. Name is "" when the service sets neither (the engine's default, no restart).
type ServiceNetwork ¶
type ServiceNetwork struct {
Aliases []string `json:"aliases,omitempty"`
}
ServiceNetwork is a service's place on a network.
type UpOptions ¶
type UpOptions struct {
// Services limits up to these services and their dependencies.
Services []string
// ForceRecreate recreates containers even when nothing changed.
ForceRecreate bool
// Build asks to build images first (needs the builder).
Build bool
// RemoveOrphans removes containers of services no longer defined.
RemoveOrphans bool
// HealthTimeout bounds the wait for each service_healthy dependency
// (0: DefaultHealthTimeout).
HealthTimeout time.Duration
}
UpOptions are `up`'s.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package cli is the `wctr compose` command tree: an exported cobra command the engine's wctr wires as a subcommand, and that cmd/wctr-compose runs on its own.
|
Package cli is the `wctr compose` command tree: an exported cobra command the engine's wctr wires as a subcommand, and that cmd/wctr-compose runs on its own. |
|
cmd
|
|
|
wctr-compose
command
Command wctr-compose is `wctr compose` on its own: the same command tree (package cli), until the engine's wctr wires it as a subcommand.
|
Command wctr-compose is `wctr compose` on its own: the same command tree (package cli), until the engine's wctr wires it as a subcommand. |
|
Package dotenv reads env files in the Compose Specification's format (container_spec.md, "Env_file format"): `VAR=VAL` lines, `#` comments, unquoted and double-quoted values interpolated, single-quoted values literal, `\` escapes, an optional `export ` prefix; a line with a bare `VAR` takes VAR from the lookup, or leaves it unset.
|
Package dotenv reads env files in the Compose Specification's format (container_spec.md, "Env_file format"): `VAR=VAL` lines, `#` comments, unquoted and double-quoted values interpolated, single-quoted values literal, `\` escapes, an optional `export ` prefix; a line with a bare `VAR` takes VAR from the lookup, or leaves it unset. |
|
Package imagebuild builds the images of a Compose project's `build:` sections with go-wasmcontainers/build, through the engine's API: the context is read from the project's file system (never outside it), the Containerfile (or Dockerfile, or dockerfile_inline) with it.
|
Package imagebuild builds the images of a Compose project's `build:` sections with go-wasmcontainers/build, through the engine's API: the context is read from the project's file system (never outside it), the Containerfile (or Dockerfile, or dockerfile_inline) with it. |
|
Package interp implements the Compose Specification's variable interpolation: `$VAR`, `${VAR}`, `${VAR:-default}`, `${VAR-default}`, `${VAR:?error}`, `${VAR?error}`, and the reference implementation's `${VAR:+replacement}` and `${VAR+replacement}`; `$$` is a literal `$`.
|
Package interp implements the Compose Specification's variable interpolation: `$VAR`, `${VAR}`, `${VAR:-default}`, `${VAR-default}`, `${VAR:?error}`, `${VAR?error}`, and the reference implementation's `${VAR:+replacement}` and `${VAR+replacement}`; `$$` is a literal `$`. |
|
Package schema embeds the Compose Specification's JSON schema (compose-spec/compose-spec, schema/compose-spec.json, Apache-2.0; see LICENSE in this directory) and answers which attributes it defines where.
|
Package schema embeds the Compose Specification's JSON schema (compose-spec/compose-spec, schema/compose-spec.json, Apache-2.0; see LICENSE in this directory) and answers which attributes it defines where. |
|
Package tree is the format-neutral document a Compose file is read into: mappings (ordered, unique keys), sequences and scalars, each carrying the file, line and column it came from.
|
Package tree is the format-neutral document a Compose file is read into: mappings (ordered, unique keys), sequences and scalars, each carrying the file, line and column it came from. |