Repository: docker/compose (go)

Issue:
`docker config --hash` output does not match `com.docker.compose.config-hash` container label
Mostly copied from my comment [here](https://github.com/docker/compose-cli/issues/935#issuecomment-1019066293).

While the hash scheme used by `--hash` subcommand for `config`/`convert` is the same, the result it produces never matches the hash stored in the `com.docker.compose.config-hash` container label in compose V2 - a regression from V1.

With `docker-compose`, it used to be possible to compare the result of `docker-compose config --hash=<service name>` to the config hash stored in the container label to determine if the config had been changed since the container had started, and needed recreating (to show as a status or warning).

As best as I can tell, this is due to `docker compose` changing the Service object in various ways before hashing it for the container label, which are not replicated before hashing it for the `docker compose config --hash` subcommand. For example, in some testing I found that just before the Service object is hashed either for storing in the container label ([here](https://github.com/docker/compose/blob/bfd7428619e7c0ca9aaedebaaa14ac0fc82e04b4/pkg/compose/create.go#L430)) or comparing against that label ([here](https://github.com/docker/compose/blob/bfd7428619e7c0ca9aaedebaaa14ac0fc82e04b4/pkg/compose/convergence.go#L196)), the digest of the available image has been added as a label in the Service config ([here](https://github.com/docker/compose/blob/bfd7428619e7c0ca9aaedebaaa14ac0fc82e04b4/pkg/compose/build.go#L144)) and any relative paths have been expanded. These changes have not been made when `config --hash` generates it's hash, and so the two never actually match any more.

The path expansion probably makes sense, and could be added in as a step in `config --hash`, but I'm not sure why adding the current image digest into the Service object is necessary for detecting convergence - this digest is already stored in the `com.docker.compose.image` container label, and can be directly compared.

Whether it is by allowing these hashes to match, or by implementing a completely new dry run system like [#8551](https://github.com/docker/compose/issues/8551) (though I suspect matching the hash mechanisms will be easier), providing a mechanism for external tools to determine if a container is running from an outdated config would be very useful.

Pull request description:
exclude com.docker.compose.image label from service hash
**What I did**
set `com.docker.compose.image label` as a custom label so it get excluded from config hash computation

**Related issue**
closes #9211

**(not mandatory) A picture of a cute animal, if possible in relation with what you did**


Patch:
diff --git a/cmd/compose/down.go b/cmd/compose/down.go
index 4c0369de9..ba0279f38 100644
--- a/cmd/compose/down.go
+++ b/cmd/compose/down.go
@@ -19,8 +19,6 @@ package compose
 import (
 	"context"
 	"fmt"
-	"os"
-	"strings"
 	"time"
 
 	"github.com/compose-spec/compose-go/types"
@@ -62,8 +60,7 @@ func downCommand(p *projectOptions, backend api.Service) *cobra.Command {
 		ValidArgsFunction: noCompletion(),
 	}
 	flags := downCmd.Flags()
-	removeOrphans := strings.ToLower(os.Getenv("COMPOSE_REMOVE_ORPHANS ")) == "true"
-	flags.BoolVar(&opts.removeOrphans, "remove-orphans", removeOrphans, "Remove containers for services not defined in the Compose file.")
+	flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
 	flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds")
 	flags.BoolVarP(&opts.volumes, "volumes", "v", false, " Remove named volumes declared in the `volumes` section of the Compose file and anonymous volumes attached to containers.")
 	flags.StringVar(&opts.images, "rmi", "", `Remove images used by services. "local" remove only images that don't have a custom tag ("local"|"all")`)
diff --git a/cmd/compose/remove.go b/cmd/compose/remove.go
index b4b61d580..f84621387 100644
--- a/cmd/compose/remove.go
+++ b/cmd/compose/remove.go
@@ -65,7 +65,7 @@ func runRemove(ctx context.Context, backend api.Service, opts removeOptions, ser
 	}
 
 	if opts.stop {
-		err := backend.Stop(ctx, project.Name, api.StopOptions{
+		err := backend.Stop(ctx, project, api.StopOptions{
 			Services: services,
 		})
 		if err != nil {
diff --git a/cmd/compose/restart.go b/cmd/compose/restart.go
index f691c31fb..600d2b6fa 100644
--- a/cmd/compose/restart.go
+++ b/cmd/compose/restart.go
@@ -49,13 +49,13 @@ func restartCommand(p *projectOptions, backend api.Service) *cobra.Command {
 }
 
 func runRestart(ctx context.Context, backend api.Service, opts restartOptions, services []string) error {
-	projectName, err := opts.toProjectName()
+	project, err := opts.toProject(services)
 	if err != nil {
 		return err
 	}
 
 	timeout := time.Duration(opts.timeout) * time.Second
-	return backend.Restart(ctx, projectName, api.RestartOptions{
+	return backend.Restart(ctx, project, api.RestartOptions{
 		Timeout:  &timeout,
 		Services: services,
 	})
diff --git a/cmd/compose/run.go b/cmd/compose/run.go
index 575026204..fc5f76421 100644
--- a/cmd/compose/run.go
+++ b/cmd/compose/run.go
@@ -240,5 +240,5 @@ func startDependencies(ctx context.Context, backend api.Service, project types.P
 	if err := backend.Create(ctx, &project, api.CreateOptions{}); err != nil {
 		return err
 	}
-	return backend.Start(ctx, project.Name, api.StartOptions{})
+	return backend.Start(ctx, &project, api.StartOptions{})
 }
diff --git a/cmd/compose/start.go b/cmd/compose/start.go
index e9b67ae44..8ad99c566 100644
--- a/cmd/compose/start.go
+++ b/cmd/compose/start.go
@@ -43,12 +43,10 @@ func startCommand(p *projectOptions, backend api.Service) *cobra.Command {
 }
 
 func runStart(ctx context.Context, backend api.Service, opts startOptions, services []string) error {
-	projectName, err := opts.toProjectName()
+	project, err := opts.toProject(services)
 	if err != nil {
 		return err
 	}
 
-	return backend.Start(ctx, projectName, api.StartOptions{
-		AttachTo: services,
-	})
+	return backend.Start(ctx, project, api.StartOptions{})
 }
diff --git a/cmd/compose/stop.go b/cmd/compose/stop.go
index 39861cd68..d00d94f9d 100644
--- a/cmd/compose/stop.go
+++ b/cmd/compose/stop.go
@@ -53,7 +53,7 @@ func stopCommand(p *projectOptions, backend api.Service) *cobra.Command {
 }
 
 func runStop(ctx context.Context, backend api.Service, opts stopOptions, services []string) error {
-	projectName, err := opts.toProjectName()
+	project, err := opts.toProject(services)
 	if err != nil {
 		return err
 	}
@@ -63,7 +63,7 @@ func runStop(ctx context.Context, backend api.Service, opts stopOptions, service
 		timeoutValue := time.Duration(opts.timeout) * time.Second
 		timeout = &timeoutValue
 	}
-	return backend.Stop(ctx, projectName, api.StopOptions{
+	return backend.Stop(ctx, project, api.StopOptions{
 		Timeout:  timeout,
 		Services: services,
 	})
diff --git a/pkg/api/api.go b/pkg/api/api.go
index 2c9e26802..e28cf12a2 100644
--- a/pkg/api/api.go
+++ b/pkg/api/api.go
@@ -37,11 +37,11 @@ type Service interface {
 	// Create executes the equivalent to a `compose create`
 	Create(ctx context.Context, project *types.Project, opts CreateOptions) error
 	// Start executes the equivalent to a `compose start`
-	Start(ctx context.Context, projectName string, options StartOptions) error
+	Start(ctx context.Context, project *types.Project, options StartOptions) error
 	// Restart restarts containers
-	Restart(ctx context.Context, projectName string, options RestartOptions) error
+	Restart(ctx context.Context, project *types.Project, options RestartOptions) error
 	// Stop executes the equivalent to a `compose stop`
-	Stop(ctx context.Context, projectName string, options StopOptions) error
+	Stop(ctx context.Context, project *types.Project, options StopOptions) error
 	// Up executes the equivalent to a `compose up`
 	Up(ctx context.Context, project *types.Project, options UpOptions) error
 	// Down executes the equivalent to a `compose down`
diff --git a/pkg/api/proxy.go b/pkg/api/proxy.go
index 9a06b9cb4..6c3fe22f3 100644
--- a/pkg/api/proxy.go
+++ b/pkg/api/proxy.go
@@ -28,9 +28,9 @@ type ServiceProxy struct {
 	PushFn               func(ctx context.Context, project *types.Project, options PushOptions) error
 	PullFn               func(ctx context.Context, project *types.Project, opts PullOptions) error
 	CreateFn             func(ctx context.Context, project *types.Project, opts CreateOptions) error
-	StartFn              func(ctx context.Context, projectName string, options StartOptions) error
-	RestartFn            func(ctx context.Context, projectName string, options RestartOptions) error
-	StopFn               func(ctx context.Context, projectName string, options StopOptions) error
+	StartFn              func(ctx context.Context, project *types.Project, options StartOptions) error
+	RestartFn            func(ctx context.Context, project *types.Project, options RestartOptions) error
+	StopFn               func(ctx context.Context, project *types.Project, options StopOptions) error
 	UpFn                 func(ctx context.Context, project *types.Project, options UpOptions) error
 	DownFn               func(ctx context.Context, projectName string, options DownOptions) error
 	LogsFn               func(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error
@@ -141,27 +141,36 @@ func (s *ServiceProxy) Create(ctx context.Context, project *types.Project, optio
 }
 
 // Start implements Service interface
-func (s *ServiceProxy) Start(ctx context.Context, projectName string, options StartOptions) error {
+func (s *ServiceProxy) Start(ctx context.Context, project *types.Project, options StartOptions) error {
 	if s.StartFn == nil {
 		return ErrNotImplemented
 	}
-	return s.StartFn(ctx, projectName, options)
+	for _, i := range s.interceptors {
+		i(ctx, project)
+	}
+	return s.StartFn(ctx, project, options)
 }
 
 // Restart implements Service interface
-func (s *ServiceProxy) Restart(ctx context.Context, projectName string, options RestartOptions) error {
+func (s *ServiceProxy) Restart(ctx context.Context, project *types.Project, options RestartOptions) error {
 	if s.RestartFn == nil {
 		return ErrNotImplemented
 	}
-	return s.RestartFn(ctx, projectName, options)
+	for _, i := range s.interceptors {
+		i(ctx, project)
+	}
+	return s.RestartFn(ctx, project, options)
 }
 
 // Stop implements Service interface
-func (s *ServiceProxy) Stop(ctx context.Context, projectName string, options StopOptions) error {
+func (s *ServiceProxy) Stop(ctx context.Context, project *types.Project, options StopOptions) error {
 	if s.StopFn == nil {
 		return ErrNotImplemented
 	}
-	return s.StopFn(ctx, projectName, options)
+	for _, i := range s.interceptors {
+		i(ctx, project)
+	}
+	return s.StopFn(ctx, project, options)
 }
 
 // Up implements Service interface
diff --git a/pkg/compose/build.go b/pkg/compose/build.go
index bc3d61f1d..c9bb56e0b 100644
--- a/pkg/compose/build.go
+++ b/pkg/compose/build.go
@@ -141,7 +141,7 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
 			if project.Services[i].Labels == nil {
 				project.Services[i].Labels = types.Labels{}
 			}
-			project.Services[i].Labels[api.ImageDigestLabel] = digest
+			project.Services[i].CustomLabels[api.ImageDigestLabel] = digest
 			project.Services[i].Image = image
 		}
 	}
diff --git a/pkg/compose/compose.go b/pkg/compose/compose.go
index 7c3c96a2b..7c31218cd 100644
--- a/pkg/compose/compose.go
+++ b/pkg/compose/compose.go
@@ -24,7 +24,6 @@ import (
 	"strings"
 
 	"github.com/docker/compose/v2/pkg/api"
-	"github.com/pkg/errors"
 
 	"github.com/compose-spec/compose-go/types"
 	"github.com/docker/cli/cli/config/configfile"
@@ -93,59 +92,3 @@ func escapeDollarSign(marshal []byte) []byte {
 	escDollar := []byte{'$', '$'}
 	return bytes.ReplaceAll(marshal, dollar, escDollar)
 }
-
-// projectFromName builds a types.Project based on actual resources with compose labels set
-func (s *composeService) projectFromName(containers Containers, projectName string, services ...string) (*types.Project, error) {
-	project := &types.Project{
-		Name: projectName,
-	}
-	if len(containers) == 0 {
-		return project, errors.New("no such project: " + projectName)
-	}
-	set := map[string]*types.ServiceConfig{}
-	for _, c := range containers {
-		serviceLabel := c.Labels[api.ServiceLabel]
-		_, ok := set[serviceLabel]
-		if !ok {
-			set[serviceLabel] = &types.ServiceConfig{
-				Name:   serviceLabel,
-				Image:  c.Image,
-				Labels: c.Labels,
-			}
-		}
-		set[serviceLabel].Scale++
-	}
-	for _, service := range set {
-		dependencies := service.Labels[api.DependenciesLabel]
-		if len(dependencies) > 0 {
-			service.DependsOn = types.DependsOnConfig{}
-			for _, dc := range strings.Split(dependencies, ",") {
-				dcArr := strings.Split(dc, ":")
-				condition := ServiceConditionRunningOrHealthy
-				dependency := dcArr[0]
-
-				// backward compatibility
-				if len(dcArr) > 1 {
-					condition = dcArr[1]
-				}
-				service.DependsOn[dependency] = types.ServiceDependency{Condition: condition}
-			}
-		}
-		project.Services = append(project.Services, *service)
-	}
-SERVICES:
-	for _, qs := range services {
-		for _, es := range project.Services {
-			if es.Name == qs {
-				continue SERVICES
-			}
-		}
-		return project, errors.New("no such service: " + qs)
-	}
-	err := project.ForServices(services)
-	if err != nil {
-		return project, err
-	}
-
-	return project, nil
-}
diff --git a/pkg/compose/create.go b/pkg/compose/create.go
index ce2551251..d77e075db 100644
--- a/pkg/compose/create.go
+++ b/pkg/compose/create.go
@@ -432,8 +432,8 @@ func (s *composeService) prepareLabels(service types.ServiceConfig, number int)
 	labels[api.ContainerNumberLabel] = strconv.Itoa(number)
 
 	var dependencies []string
-	for s, d := range service.DependsOn {
-		dependencies = append(dependencies, s+":"+d.Condition)
+	for s := range service.DependsOn {
+		dependencies = append(dependencies, s)
 	}
 	labels[api.DependenciesLabel] = strings.Join(dependencies, ",")
 	return labels, nil
diff --git a/pkg/compose/down.go b/pkg/compose/down.go
index 60498ffa3..fda34fbd9 100644
--- a/pkg/compose/down.go
+++ b/pkg/compose/down.go
@@ -41,7 +41,6 @@ func (s *composeService) Down(ctx context.Context, projectName string, options a
 }
 
 func (s *composeService) down(ctx context.Context, projectName string, options api.DownOptions) error {
-	builtFromResources := options.Project == nil
 	w := progress.ContextWriter(ctx)
 	resourceToRemove := false
 
@@ -51,8 +50,8 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
 		return err
 	}
 
-	if builtFromResources {
-		options.Project, err = s.getProjectWithVolumes(ctx, containers, projectName)
+	if options.Project == nil {
+		options.Project, err = s.projectFromLabels(ctx, containers.filter(isNotOneOff), projectName)
 		if err != nil {
 			return err
 		}
@@ -233,9 +232,34 @@ func (s *composeService) removeContainers(ctx context.Context, w progress.Writer
 	return eg.Wait()
 }
 
-func (s *composeService) getProjectWithVolumes(ctx context.Context, containers Containers, projectName string) (*types.Project, error) {
-	containers = containers.filter(isNotOneOff)
-	project, _ := s.projectFromName(containers, projectName)
+// projectFromLabels builds a types.Project based on actual resources with compose labels set
+func (s *composeService) projectFromLabels(ctx context.Context, containers Containers, projectName string) (*types.Project, error) {
+	project := &types.Project{
+		Name: projectName,
+	}
+	if len(containers) == 0 {
+		return project, nil
+	}
+	set := map[string]moby.Container{}
+	for _, c := range containers {
+		set[c.Labels[api.ServiceLabel]] = c
+	}
+	for s, c := range set {
+		service := types.ServiceConfig{
+			Name:   s,
+			Image:  c.Image,
+			Labels: c.Labels,
+		}
+		dependencies := c.Labels[api.DependenciesLabel]
+		if len(dependencies) > 0 {
+			service.DependsOn = types.DependsOnConfig{}
+			for _, d := range strings.Split(dependencies, ",") {
+				service.DependsOn[d] = types.ServiceDependency{}
+			}
+		}
+		project.Services = append(project.Services, service)
+	}
+
 	volumes, err := s.apiClient.VolumeList(ctx, filters.NewArgs(projectFilter(projectName)))
 	if err != nil {
 		return nil, err
@@ -249,5 +273,6 @@ func (s *composeService) getProjectWithVolumes(ctx context.Context, containers C
 			Labels: vol.Labels,
 		}
 	}
+
 	return project, nil
 }
diff --git a/pkg/compose/restart.go b/pkg/compose/restart.go
index 306b4d413..44592c62f 100644
--- a/pkg/compose/restart.go
+++ b/pkg/compose/restart.go
@@ -19,6 +19,7 @@ package compose
 import (
 	"context"
 
+	"github.com/compose-spec/compose-go/types"
 	"github.com/docker/compose/v2/pkg/api"
 	"golang.org/x/sync/errgroup"
 
@@ -26,20 +27,14 @@ import (
 	"github.com/docker/compose/v2/pkg/utils"
 )
 
-func (s *composeService) Restart(ctx context.Context, projectName string, options api.RestartOptions) error {
+func (s *composeService) Restart(ctx context.Context, project *types.Project, options api.RestartOptions) error {
 	return progress.Run(ctx, func(ctx context.Context) error {
-		return s.restart(ctx, projectName, options)
+		return s.restart(ctx, project, options)
 	})
 }
 
-func (s *composeService) restart(ctx context.Context, projectName string, options api.RestartOptions) error {
-
-	observedState, err := s.getContainers(ctx, projectName, oneOffInclude, true)
-	if err != nil {
-		return err
-	}
-
-	project, err := s.projectFromName(observedState, projectName, options.Services...)
+func (s *composeService) restart(ctx context.Context, project *types.Project, options api.RestartOptions) error {
+	observedState, err := s.getContainers(ctx, project.Name, oneOffInclude, true)
 	if err != nil {
 		return err
 	}
diff --git a/pkg/compose/start.go b/pkg/compose/start.go
index dca5358eb..de31b3ea7 100644
--- a/pkg/compose/start.go
+++ b/pkg/compose/start.go
@@ -28,22 +28,15 @@ import (
 	"github.com/docker/compose/v2/pkg/progress"
 )
 
-func (s *composeService) Start(ctx context.Context, projectName string, options api.StartOptions) error {
+func (s *composeService) Start(ctx context.Context, project *types.Project, options api.StartOptions) error {
 	return progress.Run(ctx, func(ctx context.Context) error {
-		return s.start(ctx, projectName, options, nil)
+		return s.start(ctx, project, options, nil)
 	})
 }
 
-func (s *composeService) start(ctx context.Context, projectName string, options api.StartOptions, listener api.ContainerEventListener) error {
-	var containers Containers
-	containers, err := s.getContainers(ctx, projectName, oneOffExclude, true)
-	if err != nil {
-		return err
-	}
-
-	project, err := s.projectFromName(containers, projectName, options.AttachTo...)
-	if err != nil {
-		return err
+func (s *composeService) start(ctx context.Context, project *types.Project, options api.StartOptions, listener api.ContainerEventListener) error {
+	if len(options.AttachTo) == 0 {
+		options.AttachTo = project.ServiceNames()
 	}
 
 	eg, ctx := errgroup.WithContext(ctx)
@@ -60,7 +53,7 @@ func (s *composeService) start(ctx context.Context, projectName string, options
 		})
 	}
 
-	err = InDependencyOrder(ctx, project, func(c context.Context, name string) error {
+	err := InDependencyOrder(ctx, project, func(c context.Context, name string) error {
 		service, err := project.GetService(name)
 		if err != nil {
 			return err
diff --git a/pkg/compose/stop.go b/pkg/compose/stop.go
index 9f3943783..6655aa2b1 100644
--- a/pkg/compose/stop.go
+++ b/pkg/compose/stop.go
@@ -21,29 +21,25 @@ import (
 
 	"github.com/docker/compose/v2/pkg/api"
 	"github.com/docker/compose/v2/pkg/progress"
+
+	"github.com/compose-spec/compose-go/types"
 )
 
-func (s *composeService) Stop(ctx context.Context, projectName string, options api.StopOptions) error {
+func (s *composeService) Stop(ctx context.Context, project *types.Project, options api.StopOptions) error {
 	return progress.Run(ctx, func(ctx context.Context) error {
-		return s.stop(ctx, projectName, options)
+		return s.stop(ctx, project, options)
 	})
 }
 
-func (s *composeService) stop(ctx context.Context, projectName string, options api.StopOptions) error {
+func (s *composeService) stop(ctx context.Context, project *types.Project, options api.StopOptions) error {
 	w := progress.ContextWriter(ctx)
 
 	services := options.Services
 	if len(services) == 0 {
-		services = []string{}
+		services = project.ServiceNames()
 	}
-
 	var containers Containers
-	containers, err := s.getContainers(ctx, projectName, oneOffInclude, true, services...)
-	if err != nil {
-		return err
-	}
-
-	project, err := s.projectFromName(containers, projectName, services...)
+	containers, err := s.getContainers(ctx, project.Name, oneOffInclude, true, services...)
 	if err != nil {
 		return err
 	}
diff --git a/pkg/compose/up.go b/pkg/compose/up.go
index 3ee07f1cc..88d0df1f8 100644
--- a/pkg/compose/up.go
+++ b/pkg/compose/up.go
@@ -38,7 +38,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
 			return err
 		}
 		if options.Start.Attach == nil {
-			return s.start(ctx, project.Name, options.Start, nil)
+			return s.start(ctx, project, options.Start, nil)
 		}
 		return nil
 	})
@@ -65,7 +65,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
 				})
 			}()
 
-			return s.Stop(ctx, project.Name, api.StopOptions{
+			return s.Stop(ctx, project, api.StopOptions{
 				Services: options.Create.Services,
 			})
 		})
@@ -85,7 +85,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
 		return err
 	})
 
-	err = s.start(ctx, project.Name, options.Start, printer.HandleEvent)
+	err = s.start(ctx, project, options.Start, printer.HandleEvent)
 	if err != nil {
 		return err
 	}


Test patch:
diff --git a/pkg/compose/stop_test.go b/pkg/compose/stop_test.go
index 29cecbd3f..4b8e345b6 100644
--- a/pkg/compose/stop_test.go
+++ b/pkg/compose/stop_test.go
@@ -25,6 +25,7 @@ import (
 	compose "github.com/docker/compose/v2/pkg/api"
 	"github.com/docker/compose/v2/pkg/mocks"
 
+	"github.com/compose-spec/compose-go/types"
 	moby "github.com/docker/docker/api/types"
 	"github.com/golang/mock/gomock"
 	"gotest.tools/v3/assert"
@@ -49,7 +50,13 @@ func TestStopTimeout(t *testing.T) {
 	api.EXPECT().ContainerStop(gomock.Any(), "456", &timeout).Return(nil)
 	api.EXPECT().ContainerStop(gomock.Any(), "789", &timeout).Return(nil)
 
-	err := tested.Stop(ctx, strings.ToLower(testProject), compose.StopOptions{
+	err := tested.Stop(ctx, &types.Project{
+		Name: strings.ToLower(testProject),
+		Services: []types.ServiceConfig{
+			{Name: "service1"},
+			{Name: "service2"},
+		},
+	}, compose.StopOptions{
 		Timeout: &timeout,
 	})
 	assert.NilError(t, err)