Skip to content

Commit cd94069

Browse files
authored
Merge pull request docker#9049 from ndeloof/9044
(re)start should not impact one-off containers
2 parents 8e7b658 + 96b152f commit cd94069

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

pkg/compose/restart.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (s *composeService) Restart(ctx context.Context, projectName string, option
3535

3636
func (s *composeService) restart(ctx context.Context, projectName string, options api.RestartOptions) error {
3737

38-
observedState, err := s.getContainers(ctx, projectName, oneOffInclude, true)
38+
observedState, err := s.getContainers(ctx, projectName, oneOffExclude, true)
3939
if err != nil {
4040
return err
4141
}

pkg/compose/stop.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func (s *composeService) stop(ctx context.Context, projectName string, options a
3939
}
4040

4141
return InReverseDependencyOrder(ctx, project, func(c context.Context, service string) error {
42-
return s.stopContainers(ctx, w, containers.filter(isService(service)), options.Timeout)
42+
containersToStop := containers.filter(isService(service)).filter(isNotOneOff)
43+
return s.stopContainers(ctx, w, containersToStop, options.Timeout)
4344
})
4445
}

pkg/e2e/start_stop_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,54 @@ func TestStartStopWithDependencies(t *testing.T) {
145145
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
146146
})
147147
}
148+
149+
func TestStartStopWithOneOffs(t *testing.T) {
150+
c := NewParallelCLI(t)
151+
const projectName = "e2e-start-stop-with-oneoffs"
152+
153+
t.Run("Up", func(t *testing.T) {
154+
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/dependencies/compose.yaml", "--project-name", projectName,
155+
"up", "-d")
156+
assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-start-stop-with-oneoffs-foo-1 Started"), res.Combined())
157+
assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-start-stop-with-oneoffs-bar-1 Started"), res.Combined())
158+
})
159+
160+
t.Run("run one-off", func(t *testing.T) {
161+
c.RunDockerComposeCmd(t, "-f", "./fixtures/dependencies/compose.yaml", "--project-name", projectName, "run", "-d", "bar", "sleep", "infinity")
162+
res := c.RunDockerComposeCmd(t, "--project-name", projectName, "ps", "-a")
163+
assert.Assert(t, strings.Contains(res.Combined(), "e2e-start-stop-with-oneoffs-foo-1"), res.Combined())
164+
assert.Assert(t, strings.Contains(res.Combined(), "e2e-start-stop-with-oneoffs-bar-1"), res.Combined())
165+
assert.Assert(t, strings.Contains(res.Combined(), "e2e-start-stop-with-oneoffs_bar_run"), res.Combined())
166+
})
167+
168+
t.Run("stop (not one-off containers)", func(t *testing.T) {
169+
res := c.RunDockerComposeCmd(t, "--project-name", projectName, "stop")
170+
assert.Assert(t, strings.Contains(res.Combined(), "e2e-start-stop-with-oneoffs-foo-1"), res.Combined())
171+
assert.Assert(t, strings.Contains(res.Combined(), "e2e-start-stop-with-oneoffs-bar-1"), res.Combined())
172+
assert.Assert(t, !strings.Contains(res.Combined(), "e2e_start_stop_with_oneoffs_bar_run"), res.Combined())
173+
174+
res = c.RunDockerComposeCmd(t, "--project-name", projectName, "ps", "-a", "--status", "running")
175+
assert.Assert(t, strings.Contains(res.Combined(), "e2e-start-stop-with-oneoffs_bar_run"), res.Combined())
176+
})
177+
178+
t.Run("start (not one-off containers)", func(t *testing.T) {
179+
res := c.RunDockerComposeCmd(t, "--project-name", projectName, "start")
180+
assert.Assert(t, strings.Contains(res.Combined(), "e2e-start-stop-with-oneoffs-foo-1"), res.Combined())
181+
assert.Assert(t, strings.Contains(res.Combined(), "e2e-start-stop-with-oneoffs-bar-1"), res.Combined())
182+
assert.Assert(t, !strings.Contains(res.Combined(), "e2e-start-stop-with-oneoffs_bar_run"), res.Combined())
183+
})
184+
185+
t.Run("restart (not one-off containers)", func(t *testing.T) {
186+
res := c.RunDockerComposeCmd(t, "--project-name", projectName, "restart")
187+
assert.Assert(t, strings.Contains(res.Combined(), "e2e-start-stop-with-oneoffs-foo-1"), res.Combined())
188+
assert.Assert(t, strings.Contains(res.Combined(), "e2e-start-stop-with-oneoffs-bar-1"), res.Combined())
189+
assert.Assert(t, !strings.Contains(res.Combined(), "e2e-start-stop-with-oneoffs_bar_run"), res.Combined())
190+
})
191+
192+
t.Run("down", func(t *testing.T) {
193+
c.RunDockerComposeCmd(t, "--project-name", projectName, "down", "--remove-orphans")
194+
195+
res := c.RunDockerComposeCmd(t, "--project-name", projectName, "ps", "-a", "--status", "running")
196+
assert.Assert(t, !strings.Contains(res.Combined(), "e2e-start-stop-with-oneoffs-bar"), res.Combined())
197+
})
198+
}

0 commit comments

Comments
 (0)