Skip to content

Commit

Permalink
Merge pull request #360 from 89luca89/fix/implement_volume_deletion
Browse files Browse the repository at this point in the history
fix: implement volume deletion when removing a devcontainer
  • Loading branch information
pascalbreuninger authored May 31, 2023
2 parents 13df43c + bbf7258 commit 87ce838
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
22 changes: 22 additions & 0 deletions pkg/docker/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ func (r *DockerHelper) FindDevContainer(labels []string) (*config.ContainerDetai
return nil, nil
}

func (r *DockerHelper) DeleteVolume(ctx context.Context, volume string) error {
if volume == "" {
return nil
}

// If volume does not exist, just exit
out, err := r.buildCmd(ctx, "volume", "list", "-q", "--filter", "name="+volume).CombinedOutput()
if err != nil {
return nil
}
if len(out) == 0 {
return nil
}

out, err = r.buildCmd(ctx, "volume", "rm", volume).CombinedOutput()
if err != nil {
return perrors.Wrapf(err, "%s", string(out))
}

return nil
}

func (r *DockerHelper) Stop(ctx context.Context, id string) error {
out, err := r.buildCmd(ctx, "stop", id).CombinedOutput()
if err != nil {
Expand Down
25 changes: 23 additions & 2 deletions pkg/driver/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,30 @@ func (d *dockerDriver) StartDevContainer(ctx context.Context, id string, labels
}

func (d *dockerDriver) DeleteDevContainer(ctx context.Context, id string, deleteVolumes bool) error {
// TODO: implement deleteVolumes
var volume string

return d.Docker.Remove(ctx, id)
if deleteVolumes {
// inspect container deleteVolumes
container, err := d.Docker.InspectContainers([]string{id})
if err != nil {
return err
}
if len(container) == 0 {
return errors.Errorf("cannot find container")
}
volume = container[0].Config.Labels["dev.containers.id"]
}

err := d.Docker.Remove(ctx, id)
if err != nil {
return err
}

if deleteVolumes {
return d.Docker.DeleteVolume(ctx, volume)
}

return nil
}

func (d *dockerDriver) StopDevContainer(ctx context.Context, id string) error {
Expand Down

0 comments on commit 87ce838

Please sign in to comment.