diff --git a/cmd/compose/restart.go b/cmd/compose/restart.go index 90848cd4..5c87616f 100644 --- a/cmd/compose/restart.go +++ b/cmd/compose/restart.go @@ -20,7 +20,6 @@ import ( "context" "time" - "github.com/compose-spec/compose-go/types" "github.com/spf13/cobra" "github.com/docker/compose/v2/pkg/api" @@ -61,22 +60,23 @@ func runRestart(ctx context.Context, backend api.Service, opts restartOptions, s return err } + if project != nil && len(services) > 0 { + err := project.EnableServices(services...) + if err != nil { + return err + } + } + var timeout *time.Duration if opts.timeChanged { timeoutValue := time.Duration(opts.timeout) * time.Second timeout = &timeoutValue } - if opts.noDeps { - err := project.ForServices(services, types.IgnoreDependencies) - if err != nil { - return err - } - } - return backend.Restart(ctx, name, api.RestartOptions{ Timeout: timeout, Services: services, Project: project, + NoDeps: opts.noDeps, }) } diff --git a/pkg/api/api.go b/pkg/api/api.go index b1442eb5..28595941 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -213,6 +213,8 @@ type RestartOptions struct { Timeout *time.Duration // Services passed in the command line to be restarted Services []string + // NoDeps ignores services dependencies + NoDeps bool } // StopOptions group options of the Stop API diff --git a/pkg/compose/restart.go b/pkg/compose/restart.go index 72cf0a81..db27b1fb 100644 --- a/pkg/compose/restart.go +++ b/pkg/compose/restart.go @@ -48,6 +48,13 @@ func (s *composeService) restart(ctx context.Context, projectName string, option } } + if options.NoDeps { + err := project.ForServices(options.Services, types.IgnoreDependencies) + if err != nil { + return err + } + } + // ignore depends_on relations which are not impacted by restarting service or not required for i, service := range project.Services { for name, r := range service.DependsOn { diff --git a/pkg/e2e/fixtures/restart-test/compose.yaml b/pkg/e2e/fixtures/restart-test/compose.yaml index 9055af4e..92c28d3a 100644 --- a/pkg/e2e/fixtures/restart-test/compose.yaml +++ b/pkg/e2e/fixtures/restart-test/compose.yaml @@ -3,3 +3,10 @@ services: image: alpine init: true command: ash -c "if [[ -f /tmp/restart.lock ]] ; then sleep infinity; else touch /tmp/restart.lock; fi" + + test: + profiles: + - test + image: alpine + init: true + command: ash -c "if [[ -f /tmp/restart.lock ]] ; then sleep infinity; else touch /tmp/restart.lock; fi" diff --git a/pkg/e2e/restart_test.go b/pkg/e2e/restart_test.go index 6d00159b..91ade0b3 100644 --- a/pkg/e2e/restart_test.go +++ b/pkg/e2e/restart_test.go @@ -84,3 +84,19 @@ func TestRestartWithDependencies(t *testing.T) { assert.Assert(t, strings.Contains(res.Combined(), fmt.Sprintf("Container e2e-restart-deps-%s-1 Started", depWithRestart)), res.Combined()) assert.Assert(t, !strings.Contains(res.Combined(), depNoRestart), res.Combined()) } + +func TestRestartWithProfiles(t *testing.T) { + c := NewParallelCLI(t, WithEnv( + "COMPOSE_PROJECT_NAME=e2e-restart-profiles", + )) + + t.Cleanup(func() { + c.RunDockerComposeCmd(t, "down", "--remove-orphans") + }) + + c.RunDockerComposeCmd(t, "-f", "./fixtures/restart-test/compose.yaml", "--profile", "test", "up", "-d") + + res := c.RunDockerComposeCmd(t, "restart", "test") + fmt.Println(res.Combined()) + assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-restart-profiles-test-1 Started"), res.Combined()) +}