mirror of
https://github.com/docker/compose.git
synced 2025-07-21 20:54:32 +02:00
Merge pull request #10338 from glours/fix-restart-depends_on
restart only needed services by checking depends_on relations
This commit is contained in:
commit
a84345be8f
@ -21,6 +21,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/compose-spec/compose-go/types"
|
"github.com/compose-spec/compose-go/types"
|
||||||
@ -181,13 +182,18 @@ func (s *composeService) projectFromName(containers Containers, projectName stri
|
|||||||
for _, dc := range strings.Split(dependencies, ",") {
|
for _, dc := range strings.Split(dependencies, ",") {
|
||||||
dcArr := strings.Split(dc, ":")
|
dcArr := strings.Split(dc, ":")
|
||||||
condition := ServiceConditionRunningOrHealthy
|
condition := ServiceConditionRunningOrHealthy
|
||||||
|
// Let's restart the dependency by default if we don't have the info stored in the label
|
||||||
|
restart := true
|
||||||
dependency := dcArr[0]
|
dependency := dcArr[0]
|
||||||
|
|
||||||
// backward compatibility
|
// backward compatibility
|
||||||
if len(dcArr) > 1 {
|
if len(dcArr) > 1 {
|
||||||
condition = dcArr[1]
|
condition = dcArr[1]
|
||||||
|
if len(dcArr) > 2 {
|
||||||
|
restart, _ = strconv.ParseBool(dcArr[2])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
service.DependsOn[dependency] = types.ServiceDependency{Condition: condition}
|
service.DependsOn[dependency] = types.ServiceDependency{Condition: condition, Restart: restart}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
project.Services = append(project.Services, *service)
|
project.Services = append(project.Services, *service)
|
||||||
|
@ -470,7 +470,7 @@ func (s *composeService) prepareLabels(service types.ServiceConfig, number int)
|
|||||||
|
|
||||||
var dependencies []string
|
var dependencies []string
|
||||||
for s, d := range service.DependsOn {
|
for s, d := range service.DependsOn {
|
||||||
dependencies = append(dependencies, s+":"+d.Condition)
|
dependencies = append(dependencies, fmt.Sprintf("%s:%s:%t", s, d.Condition, d.Restart))
|
||||||
}
|
}
|
||||||
labels[api.DependenciesLabel] = strings.Join(dependencies, ",")
|
labels[api.DependenciesLabel] = strings.Join(dependencies, ",")
|
||||||
return labels, nil
|
return labels, nil
|
||||||
|
@ -20,6 +20,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/compose-spec/compose-go/types"
|
||||||
"github.com/docker/compose/v2/pkg/api"
|
"github.com/docker/compose/v2/pkg/api"
|
||||||
"github.com/docker/compose/v2/pkg/progress"
|
"github.com/docker/compose/v2/pkg/progress"
|
||||||
"github.com/docker/compose/v2/pkg/utils"
|
"github.com/docker/compose/v2/pkg/utils"
|
||||||
@ -57,8 +58,8 @@ func (s *composeService) restart(ctx context.Context, projectName string, option
|
|||||||
project.Services[i] = service
|
project.Services[i] = service
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(options.Services) == 0 {
|
if len(options.Services) != 0 {
|
||||||
err = project.ForServices(options.Services)
|
err = project.ForServices(options.Services, types.IncludeDependents)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
20
pkg/e2e/fixtures/restart-test/compose-depends-on.yaml
Normal file
20
pkg/e2e/fixtures/restart-test/compose-depends-on.yaml
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
services:
|
||||||
|
with-restart:
|
||||||
|
image: alpine
|
||||||
|
command: tail -f /dev/null
|
||||||
|
depends_on:
|
||||||
|
nginx: {condition: service_healthy, restart: true}
|
||||||
|
|
||||||
|
no-restart:
|
||||||
|
image: alpine
|
||||||
|
command: tail -f /dev/null
|
||||||
|
depends_on:
|
||||||
|
nginx: { condition: service_healthy }
|
||||||
|
|
||||||
|
nginx:
|
||||||
|
image: nginx:alpine
|
||||||
|
healthcheck:
|
||||||
|
test: "echo | nc -w 5 localhost:80"
|
||||||
|
interval: 2s
|
||||||
|
timeout: 1s
|
||||||
|
retries: 10
|
@ -63,3 +63,24 @@ func TestRestart(t *testing.T) {
|
|||||||
c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
|
c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRestartWithDependencies(t *testing.T) {
|
||||||
|
c := NewParallelCLI(t, WithEnv(
|
||||||
|
"COMPOSE_PROJECT_NAME=e2e-restart-deps",
|
||||||
|
))
|
||||||
|
baseService := "nginx"
|
||||||
|
depWithRestart := "with-restart"
|
||||||
|
depNoRestart := "no-restart"
|
||||||
|
|
||||||
|
t.Cleanup(func() {
|
||||||
|
c.RunDockerComposeCmd(t, "down", "--remove-orphans")
|
||||||
|
})
|
||||||
|
|
||||||
|
c.RunDockerComposeCmd(t, "-f", "./fixtures/restart-test/compose-depends-on.yaml", "up", "-d")
|
||||||
|
|
||||||
|
res := c.RunDockerComposeCmd(t, "restart", baseService)
|
||||||
|
fmt.Println(res.Combined())
|
||||||
|
assert.Assert(t, strings.Contains(res.Combined(), fmt.Sprintf("Container e2e-restart-deps-%s-1 Started", baseService)), res.Combined())
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user