compose/pkg/compose/containers.go
Guillaume Lours 9d444a667a
Some checks failed
ci / validate (lint) (push) Has been cancelled
ci / validate (validate-docs) (push) Has been cancelled
ci / validate (validate-go-mod) (push) Has been cancelled
ci / validate (validate-headers) (push) Has been cancelled
ci / binary (push) Has been cancelled
ci / bin-image-test (push) Has been cancelled
ci / test (push) Has been cancelled
ci / e2e (plugin, oldstable) (push) Has been cancelled
ci / e2e (standalone, oldstable) (push) Has been cancelled
ci / e2e (plugin, stable) (push) Has been cancelled
ci / e2e (standalone, stable) (push) Has been cancelled
merge / bin-image-prepare (push) Has been cancelled
merge / module-image (push) Has been cancelled
Scorecards supply-chain security / Scorecards analysis (push) Has been cancelled
ci / binary-finalize (push) Has been cancelled
ci / coverage (push) Has been cancelled
ci / release (push) Has been cancelled
merge / bin-image (push) Has been cancelled
merge / desktop-edge-test (push) Has been cancelled
pre_start: support pre_start lifecycle hooks
runPreStart executes a service's pre_start hooks sequentially as
ephemeral containers that share the first non-running replica's
volumes via VolumesFrom and attach to the same networks. A non-zero
hook exit gates service start.

per_replica: false is the only currently supported mode; per_replica:
true is rejected up front. The donor replica is the lowest-numbered
one so the choice is deterministic. ContainerWait uses
WaitConditionNextExit, and the wait loop deterministically handles
the daemon's clean-close (nil on Error + exit code on Result) and
transport-error races to avoid spurious hook failures.

The log stream is opened before ContainerStart to avoid racing
AutoRemove on fast-exiting hooks, and runs under a derived context
so a daemon that keeps the connection open cannot deadlock the call.
Hook containers carry project/service/version labels; the two
cleanup paths force-remove the never-started container explicitly
and warn when removal fails.

pre_start runs once per service when no replica is already running
(initial up, force-recreate or spec change), and is skipped on
scale-up so additional replicas don't re-trigger the hooks.

Coverage: 11 unit tests (including scheduler-race stress) with
goroutine-leak verification via goleak, plus 10 E2E tests (success
path, hook failure gating start, build-image inheritance, idempotent
re-up, spec change, force-recreate, mid-sequence failure, ordering,
scale-up, scaled service).

Signed-off-by: Guillaume Lours <glours@users.noreply.github.com>
2026-06-30 10:49:04 +02:00

197 lines
6.1 KiB
Go

/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package compose
import (
"context"
"fmt"
"slices"
"sort"
"strconv"
"github.com/compose-spec/compose-go/v2/types"
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/client"
"golang.org/x/sync/errgroup"
"github.com/docker/compose/v5/pkg/api"
)
// Containers is a set of moby Container
type Containers []container.Summary
type oneOff int
const (
oneOffInclude = oneOff(iota)
oneOffExclude
oneOffOnly
)
func (s *composeService) getContainers(ctx context.Context, project string, oneOff oneOff, all bool, selectedServices ...string) (Containers, error) {
res, err := s.apiClient().ContainerList(ctx, client.ContainerListOptions{
Filters: getDefaultFilters(project, oneOff, selectedServices...),
All: all,
})
if err != nil {
return nil, err
}
containers := Containers(res.Items)
if len(selectedServices) > 1 {
containers = containers.filter(isService(selectedServices...))
}
return containers, nil
}
// getContainersByService returns all non-oneoff containers for the project, grouped by service name.
func (s *composeService) getContainersByService(ctx context.Context, projectName string) (map[string]Containers, error) {
all, err := s.getContainers(ctx, projectName, oneOffExclude, true)
if err != nil {
return nil, err
}
result := map[string]Containers{}
for _, c := range all.filter(isNotOneOff) {
svc := c.Labels[api.ServiceLabel]
result[svc] = append(result[svc], c)
}
return result, nil
}
func getDefaultFilters(projectName string, oneOff oneOff, selectedServices ...string) client.Filters {
f := projectFilter(projectName)
if len(selectedServices) == 1 {
f.Add("label", serviceFilter(selectedServices[0]))
}
f.Add("label", api.ConfigHashLabel)
switch oneOff {
case oneOffOnly:
f.Add("label", oneOffFilter(true))
case oneOffExclude:
f.Add("label", oneOffFilter(false))
case oneOffInclude:
}
return f
}
func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName string, oneOff oneOff, all bool, serviceName string, containerIndex int) (container.Summary, error) {
defaultFilters := getDefaultFilters(projectName, oneOff, serviceName)
if containerIndex > 0 {
defaultFilters.Add("label", containerNumberFilter(containerIndex))
}
res, err := s.apiClient().ContainerList(ctx, client.ContainerListOptions{
Filters: defaultFilters,
All: all,
})
if err != nil {
return container.Summary{}, err
}
containers := res.Items
if len(containers) < 1 {
if containerIndex > 0 {
return container.Summary{}, fmt.Errorf("service %q is not running container #%d", serviceName, containerIndex)
}
return container.Summary{}, fmt.Errorf("service %q is not running", serviceName)
}
// Sort by container number first, then put one-off containers at the end
sort.Slice(containers, func(i, j int) bool {
numberLabelX, _ := strconv.Atoi(containers[i].Labels[api.ContainerNumberLabel])
numberLabelY, _ := strconv.Atoi(containers[j].Labels[api.ContainerNumberLabel])
IsOneOffLabelTrueX := containers[i].Labels[api.OneoffLabel] == "True"
IsOneOffLabelTrueY := containers[j].Labels[api.OneoffLabel] == "True"
if IsOneOffLabelTrueX || IsOneOffLabelTrueY {
return !IsOneOffLabelTrueX && IsOneOffLabelTrueY
}
return numberLabelX < numberLabelY
})
return containers[0], nil
}
// containerPredicate define a predicate we want container to satisfy for filtering operations
type containerPredicate func(c container.Summary) bool
func matches(c container.Summary, predicates ...containerPredicate) bool {
for _, predicate := range predicates {
if !predicate(c) {
return false
}
}
return true
}
func isService(services ...string) containerPredicate {
return func(c container.Summary) bool {
service := c.Labels[api.ServiceLabel]
return slices.Contains(services, service)
}
}
// isOrphaned is a predicate to select containers without a matching service definition in compose project
func isOrphaned(project *types.Project) containerPredicate {
services := append(project.ServiceNames(), project.DisabledServiceNames()...)
return func(c container.Summary) bool {
// One-off container
v, ok := c.Labels[api.OneoffLabel]
if ok && v == "True" {
return c.State == container.StateExited || c.State == container.StateDead
}
// Service that is not defined in the compose model
service := c.Labels[api.ServiceLabel]
return !slices.Contains(services, service)
}
}
func isNotOneOff(c container.Summary) bool {
v, ok := c.Labels[api.OneoffLabel]
return !ok || v == "False"
}
func isNotRunning(c container.Summary) bool {
return c.State != container.StateRunning
}
// filter return Containers with elements to match predicate
func (containers Containers) filter(predicates ...containerPredicate) Containers {
var filtered Containers
for _, c := range containers {
if matches(c, predicates...) {
filtered = append(filtered, c)
}
}
return filtered
}
// forEachContainerConcurrent runs fn for every container concurrently and waits for all goroutines.
func forEachContainerConcurrent(ctx context.Context, containers Containers, fn func(context.Context, container.Summary) error) error {
eg, ctx := errgroup.WithContext(ctx)
for _, ctr := range containers {
eg.Go(func() error {
return fn(ctx, ctr)
})
}
return eg.Wait()
}
// sorted sorts containers in place by canonical name and returns the (same) slice.
func (containers Containers) sorted() Containers {
sort.Slice(containers, func(i, j int) bool {
return getCanonicalContainerName(containers[i]) < getCanonicalContainerName(containers[j])
})
return containers
}