Skip to content

Commit 03bac09

Browse files
7ttpavallete
andauthored
fix: remove all containers on stop to prevent name conflicts (#4859)
* fix: remove all containers on stop to prevent name conflicts * nit --------- Co-authored-by: Andrew Valleteau <avallete@users.noreply.github.com>
1 parent d820f84 commit 03bac09

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

internal/stop/stop_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ func TestStopCommand(t *testing.T) {
163163

164164
func TestStopServices(t *testing.T) {
165165
t.Run("stops all services", func(t *testing.T) {
166-
containers := []container.Summary{{ID: "c1", State: "running"}, {ID: "c2"}}
166+
containers := []container.Summary{
167+
{ID: "c1", State: "running"},
168+
{ID: "c2", State: "exited"},
169+
}
167170
// Setup mock docker
168171
require.NoError(t, apitest.MockDocker(utils.Docker))
169172
defer gock.OffAll()
@@ -174,6 +177,9 @@ func TestStopServices(t *testing.T) {
174177
gock.New(utils.Docker.DaemonHost()).
175178
Post("/v" + utils.Docker.ClientVersion() + "/containers/" + containers[0].ID + "/stop").
176179
Reply(http.StatusOK)
180+
gock.New(utils.Docker.DaemonHost()).
181+
Post("/v" + utils.Docker.ClientVersion() + "/containers/" + containers[1].ID + "/stop").
182+
Reply(http.StatusNotModified)
177183
gock.New(utils.Docker.DaemonHost()).
178184
Post("/v" + utils.Docker.ClientVersion() + "/containers/prune").
179185
Reply(http.StatusOK).

internal/utils/docker.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,10 @@ func DockerRemoveAll(ctx context.Context, w io.Writer, projectId string) error {
106106
// Gracefully shutdown containers
107107
var ids []string
108108
for _, c := range containers {
109-
if c.State == "running" {
110-
ids = append(ids, c.ID)
111-
}
109+
ids = append(ids, c.ID)
112110
}
113111
result := WaitAll(ids, func(id string) error {
114-
if err := Docker.ContainerStop(ctx, id, container.StopOptions{}); err != nil {
112+
if err := Docker.ContainerStop(ctx, id, container.StopOptions{}); err != nil && !errdefs.IsNotModified(err) {
115113
return errors.Errorf("failed to stop container: %w", err)
116114
}
117115
return nil

0 commit comments

Comments
 (0)