Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove volumes on down #910

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion modules/compose/compose_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (io IgnoreOrphans) applyToStackUp(co *api.CreateOptions, _ *api.StartOption
co.IgnoreOrphans = bool(io)
}

// RemoveOrphans will cleanup containers that are not declared on the compose model but own the same labels
// RemoveOrphans will clean up containers that are not declared on the compose model but own the same labels
type RemoveOrphans bool

func (ro RemoveOrphans) applyToStackUp(o *stackUpOptions) {
Expand All @@ -68,6 +68,12 @@ func (w Wait) applyToStackUp(o *stackUpOptions) {
o.Wait = bool(w)
}

type RemoveVolumes bool

func (ro RemoveVolumes) applyToStackDown(o *stackDownOptions) {
o.Volumes = bool(ro)
}

// RemoveImages used by services
type RemoveImages uint8

Expand Down
25 changes: 25 additions & 0 deletions modules/compose/compose_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package compose
import (
"context"
"fmt"
"github.com/docker/docker/api/types/filters"
"github.com/google/uuid"
"hash/fnv"
"testing"
"time"
Expand Down Expand Up @@ -365,6 +367,29 @@ func TestDockerComposeAPIWithVolume(t *testing.T) {
assert.NoError(t, err, "compose.Up()")
}

func TestDockerComposeAPIVolumesDeletedOnDown(t *testing.T) {
identifier := uuid.New().String()
stackFiles := WithStackFiles("./testresources/docker-compose-volume.yml")
cgoodsirsmyth-pp marked this conversation as resolved.
Show resolved Hide resolved
compose, err := NewDockerComposeWith(stackFiles, StackIdentifier(identifier))
assert.NoError(t, err, "NewDockerCompose()")

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

err = compose.Up(ctx, Wait(true))
assert.NoError(t, err, "compose.Up()")

err = compose.Down(context.Background(), RemoveOrphans(true), RemoveVolumes(true), RemoveImagesLocal)
assert.NoError(t, err, "compose.Down()")

volumeListFilters := filters.NewArgs()
volumeListFilters.Add("name", fmt.Sprintf("%s_mydata", identifier))
cgoodsirsmyth-pp marked this conversation as resolved.
Show resolved Hide resolved
cgoodsirsmyth-pp marked this conversation as resolved.
Show resolved Hide resolved
volumeList, err := compose.dockerClient.VolumeList(ctx, volumeListFilters)
assert.NoError(t, err, "compose.dockerClient.VolumeList()")

assert.Equal(t, 0, len(volumeList.Volumes), "Volumes are not cleaned up")
}

func TestDockerComposeAPIWithBuild(t *testing.T) {
compose, err := NewDockerCompose("./testresources/docker-compose-build.yml")
assert.NoError(t, err, "NewDockerCompose()")
Expand Down