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 created volumes when taking down a compose stack #690

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions compose_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func (d *dockerCompose) Down(ctx context.Context, opts ...StackDownOption) error
options := stackDownOptions{
DownOptions: api.DownOptions{
Project: d.project,
Volumes: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @andyautida14 this will always remove the volumes. Do you think we can make it optional, creating a RemoveVolumes functional option, exactly the same as in

// RemoveOrphans will cleanup containers that are not declared on the compose model but own the same labels
type RemoveOrphans bool
func (ro RemoveOrphans) applyToStackUp(o *stackUpOptions) {
o.RemoveOrphans = bool(ro)
}
func (ro RemoveOrphans) applyToStackDown(o *stackDownOptions) {
o.RemoveOrphans = bool(ro)
}
, only for the down options?

That way users would be able to configure if they need to clean up the volumes they create

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sure. I'll try to find some time this week.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW @andyautida14 please take a look at #650, which was merged after your PR: it moves the compose code to a separate module

},
}

Expand Down
25 changes: 25 additions & 0 deletions compose_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/docker/docker/api/types/filters"

"github.com/testcontainers/testcontainers-go/wait"
)
Expand Down Expand Up @@ -365,6 +367,29 @@ func TestDockerComposeAPIWithVolume(t *testing.T) {
assert.NoError(t, err, "compose.Up()")
}

func TestDockerComposeAPICleanupVolumesOnDown(t *testing.T) {
identifier := uuid.New().String()
stackFiles := WithStackFiles("./testresources/docker-compose-volume.yml")
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), RemoveImagesLocal)
assert.NoError(t, err, "compose.Down()")

volumeListFilters := filters.NewArgs()
volumeListFilters.Add("name", fmt.Sprintf("%s_mydata", identifier))
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