Skip to content

Commit 3ccf434

Browse files
authored
Merge pull request docker#9572 from laurazard/dont-wait-for-oneshots
Use appropriate dependency condition for one-shot containers when running `compose up --wait`
2 parents 2cd9c0d + 74fd14e commit 3ccf434

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

pkg/compose/start.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (s *composeService) start(ctx context.Context, projectName string, options
8080
depends := types.DependsOnConfig{}
8181
for _, s := range project.Services {
8282
depends[s.Name] = types.ServiceDependency{
83-
Condition: ServiceConditionRunningOrHealthy,
83+
Condition: getDependencyCondition(s, project),
8484
}
8585
}
8686
err = s.waitDependencies(ctx, project, depends)
@@ -92,6 +92,20 @@ func (s *composeService) start(ctx context.Context, projectName string, options
9292
return eg.Wait()
9393
}
9494

95+
// getDependencyCondition checks if service is depended on by other services
96+
// with service_completed_successfully condition, and applies that condition
97+
// instead, or --wait will never finish waiting for one-shot containers
98+
func getDependencyCondition(service types.ServiceConfig, project *types.Project) string {
99+
for _, services := range project.Services {
100+
for dependencyService, dependencyConfig := range services.DependsOn {
101+
if dependencyService == service.Name && dependencyConfig.Condition == types.ServiceConditionCompletedSuccessfully {
102+
return types.ServiceConditionCompletedSuccessfully
103+
}
104+
}
105+
}
106+
return ServiceConditionRunningOrHealthy
107+
}
108+
95109
type containerWatchFn func(container moby.Container) error
96110

97111
// watchContainers uses engine events to capture container start/die and notify ContainerEventListener

pkg/e2e/compose_up_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package e2e
18+
19+
import (
20+
"strings"
21+
"testing"
22+
"time"
23+
24+
"gotest.tools/v3/assert"
25+
)
26+
27+
func TestUpWait(t *testing.T) {
28+
c := NewParallelCLI(t)
29+
const projectName = "e2e-deps-wait"
30+
31+
timeout := time.After(30 * time.Second)
32+
done := make(chan bool)
33+
go func() {
34+
res := c.RunDockerComposeCmd(t, "-f", "fixtures/dependencies/deps-completed-successfully.yaml", "--project-name", projectName, "up", "--wait", "-d")
35+
assert.Assert(t, strings.Contains(res.Combined(), "e2e-deps-wait-oneshot-1"), res.Combined())
36+
done <- true
37+
}()
38+
39+
select {
40+
case <-timeout:
41+
t.Fatal("test did not finish in time")
42+
case <-done:
43+
break
44+
}
45+
46+
c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
47+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
oneshot:
3+
image: alpine
4+
command: echo 'hello world'
5+
longrunning:
6+
image: alpine
7+
depends_on:
8+
oneshot:
9+
condition: service_completed_successfully
10+
command: sleep infinity

0 commit comments

Comments
 (0)