diff --git a/core/provider/digitalocean/task.go b/core/provider/digitalocean/task.go index 5a22e22..228cc75 100644 --- a/core/provider/digitalocean/task.go +++ b/core/provider/digitalocean/task.go @@ -381,8 +381,16 @@ func (p *Provider) RunCommandWhileStopped(ctx context.Context, taskName string, return "", "", 0, err } - // nolint - defer dockerClient.ContainerRemove(ctx, createdContainer.ID, types.ContainerRemoveOptions{Force: true}) + defer func() { + if _, err := dockerClient.ContainerInspect(ctx, createdContainer.ID); err != nil && dockerclient.IsErrNotFound(err) { + // auto-removed, but not detected as autoremoved + return + } + + if err := dockerClient.ContainerRemove(ctx, createdContainer.ID, types.ContainerRemoveOptions{Force: true}); err != nil { + p.logger.Error("failed to remove container", zap.Error(err), zap.String("taskName", taskName), zap.String("id", createdContainer.ID)) + } + }() if err := startContainerWithBlock(ctx, dockerClient, createdContainer.ID); err != nil { p.logger.Error("failed to start container", zap.Error(err), zap.String("taskName", taskName)) diff --git a/core/provider/docker/volume.go b/core/provider/docker/volume.go index d66372d..529fff1 100644 --- a/core/provider/docker/volume.go +++ b/core/provider/docker/volume.go @@ -17,6 +17,7 @@ import ( "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/volume" + "github.com/docker/docker/client" "github.com/skip-mev/petri/core/v2/provider" ) @@ -124,11 +125,15 @@ func (p *Provider) WriteFile(ctx context.Context, id, relPath string, content [] return } - // nolint // will fix later + if _, err := p.dockerClient.ContainerInspect(ctx, cc.ID); err != nil && client.IsErrNotFound(err) { + // auto-removed, but not detected as autoremoved + return + } + if err := p.dockerClient.ContainerRemove(ctx, cc.ID, types.ContainerRemoveOptions{ Force: true, }); err != nil { - // TODO fix logging + logger.Error("failed to remove writefile container", zap.String("id", cc.ID), zap.Error(err)) } }() @@ -242,11 +247,15 @@ func (p *Provider) ReadFile(ctx context.Context, id, relPath string) ([]byte, er logger.Debug("created getfile container", zap.String("id", cc.ID)) defer func() { + if _, err := p.dockerClient.ContainerInspect(ctx, cc.ID); err != nil && client.IsErrNotFound(err) { + // auto-removed, but not detected as autoremoved + return + } + if err := p.dockerClient.ContainerRemove(ctx, cc.ID, types.ContainerRemoveOptions{ Force: true, }); err != nil { logger.Error("failed cleaning up the getfile container", zap.Error(err)) - // todo fix logging } }() @@ -328,7 +337,10 @@ func (p *Provider) DownloadDir(ctx context.Context, id, relPath, localPath strin } defer func() { - // nolint // will fix later + if _, err := p.dockerClient.ContainerInspect(ctx, cc.ID); err != nil && client.IsErrNotFound(err) { + return + } + if err := p.dockerClient.ContainerRemove(ctx, cc.ID, types.ContainerRemoveOptions{ Force: true, }); err != nil { @@ -408,8 +420,8 @@ func (p *Provider) SetVolumeOwner(ctx context.Context, volumeName, uid, gid stri User: "0", }, &container.HostConfig{ - Binds: []string{volumeName + ":" + mountPath}, - // AutoRemove: true, + Binds: []string{volumeName + ":" + mountPath}, + AutoRemove: true, }, nil, // No networking necessary. nil, @@ -425,7 +437,11 @@ func (p *Provider) SetVolumeOwner(ctx context.Context, volumeName, uid, gid stri return } - // nolint // will fix later + if _, err := p.dockerClient.ContainerInspect(ctx, cc.ID); err != nil && client.IsErrNotFound(err) { + // auto-removed, but not detected as autoremoved + return + } + if err := p.dockerClient.ContainerRemove(ctx, cc.ID, types.ContainerRemoveOptions{ Force: true, }); err != nil {