Skip to content

Commit a84345b

Browse files
authored
Merge pull request docker#10338 from glours/fix-restart-depends_on
restart only needed services by checking depends_on relations
2 parents a3bed26 + 3cfbac6 commit a84345b

File tree

5 files changed

+52
-4
lines changed

5 files changed

+52
-4
lines changed

pkg/compose/compose.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"io"
24+
"strconv"
2425
"strings"
2526

2627
"github.com/compose-spec/compose-go/types"
@@ -181,13 +182,18 @@ func (s *composeService) projectFromName(containers Containers, projectName stri
181182
for _, dc := range strings.Split(dependencies, ",") {
182183
dcArr := strings.Split(dc, ":")
183184
condition := ServiceConditionRunningOrHealthy
185+
// Let's restart the dependency by default if we don't have the info stored in the label
186+
restart := true
184187
dependency := dcArr[0]
185188

186189
// backward compatibility
187190
if len(dcArr) > 1 {
188191
condition = dcArr[1]
192+
if len(dcArr) > 2 {
193+
restart, _ = strconv.ParseBool(dcArr[2])
194+
}
189195
}
190-
service.DependsOn[dependency] = types.ServiceDependency{Condition: condition}
196+
service.DependsOn[dependency] = types.ServiceDependency{Condition: condition, Restart: restart}
191197
}
192198
}
193199
project.Services = append(project.Services, *service)

pkg/compose/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ func (s *composeService) prepareLabels(service types.ServiceConfig, number int)
470470

471471
var dependencies []string
472472
for s, d := range service.DependsOn {
473-
dependencies = append(dependencies, s+":"+d.Condition)
473+
dependencies = append(dependencies, fmt.Sprintf("%s:%s:%t", s, d.Condition, d.Restart))
474474
}
475475
labels[api.DependenciesLabel] = strings.Join(dependencies, ",")
476476
return labels, nil

pkg/compose/restart.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"strings"
2222

23+
"github.com/compose-spec/compose-go/types"
2324
"github.com/docker/compose/v2/pkg/api"
2425
"github.com/docker/compose/v2/pkg/progress"
2526
"github.com/docker/compose/v2/pkg/utils"
@@ -57,8 +58,8 @@ func (s *composeService) restart(ctx context.Context, projectName string, option
5758
project.Services[i] = service
5859
}
5960

60-
if len(options.Services) == 0 {
61-
err = project.ForServices(options.Services)
61+
if len(options.Services) != 0 {
62+
err = project.ForServices(options.Services, types.IncludeDependents)
6263
if err != nil {
6364
return err
6465
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
services:
2+
with-restart:
3+
image: alpine
4+
command: tail -f /dev/null
5+
depends_on:
6+
nginx: {condition: service_healthy, restart: true}
7+
8+
no-restart:
9+
image: alpine
10+
command: tail -f /dev/null
11+
depends_on:
12+
nginx: { condition: service_healthy }
13+
14+
nginx:
15+
image: nginx:alpine
16+
healthcheck:
17+
test: "echo | nc -w 5 localhost:80"
18+
interval: 2s
19+
timeout: 1s
20+
retries: 10

pkg/e2e/restart_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,24 @@ func TestRestart(t *testing.T) {
6363
c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
6464
})
6565
}
66+
67+
func TestRestartWithDependencies(t *testing.T) {
68+
c := NewParallelCLI(t, WithEnv(
69+
"COMPOSE_PROJECT_NAME=e2e-restart-deps",
70+
))
71+
baseService := "nginx"
72+
depWithRestart := "with-restart"
73+
depNoRestart := "no-restart"
74+
75+
t.Cleanup(func() {
76+
c.RunDockerComposeCmd(t, "down", "--remove-orphans")
77+
})
78+
79+
c.RunDockerComposeCmd(t, "-f", "./fixtures/restart-test/compose-depends-on.yaml", "up", "-d")
80+
81+
res := c.RunDockerComposeCmd(t, "restart", baseService)
82+
fmt.Println(res.Combined())
83+
assert.Assert(t, strings.Contains(res.Combined(), fmt.Sprintf("Container e2e-restart-deps-%s-1 Started", baseService)), res.Combined())
84+
assert.Assert(t, strings.Contains(res.Combined(), fmt.Sprintf("Container e2e-restart-deps-%s-1 Started", depWithRestart)), res.Combined())
85+
assert.Assert(t, !strings.Contains(res.Combined(), depNoRestart), res.Combined())
86+
}

0 commit comments

Comments
 (0)