diff --git a/pkg/container/docker_build.go b/pkg/container/docker_build.go index 537a5bd0..8bf3466e 100644 --- a/pkg/container/docker_build.go +++ b/pkg/container/docker_build.go @@ -38,6 +38,7 @@ func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor { if err != nil { return err } + defer cli.Close() logger.Debugf("Building image from '%v'", input.ContextDir) diff --git a/pkg/container/docker_images.go b/pkg/container/docker_images.go index f9dab3c5..e23699e1 100644 --- a/pkg/container/docker_images.go +++ b/pkg/container/docker_images.go @@ -15,6 +15,7 @@ func ImageExistsLocally(ctx context.Context, imageName string, platform string) if err != nil { return false, err } + defer cli.Close() filters := filters.NewArgs() filters.Add("reference", imageName) diff --git a/pkg/container/docker_pull.go b/pkg/container/docker_pull.go index 55545a80..399e4604 100644 --- a/pkg/container/docker_pull.go +++ b/pkg/container/docker_pull.go @@ -57,6 +57,7 @@ func NewDockerPullExecutor(input NewDockerPullExecutorInput) common.Executor { if err != nil { return err } + defer cli.Close() imagePullOptions, err := getImagePullOptions(ctx, input) if err != nil { diff --git a/pkg/container/docker_run.go b/pkg/container/docker_run.go index 3757d844..f3677c45 100644 --- a/pkg/container/docker_run.go +++ b/pkg/container/docker_run.go @@ -76,6 +76,7 @@ type Container interface { UpdateFromImageEnv(env *map[string]string) common.Executor UpdateFromPath(env *map[string]string) common.Executor Remove() common.Executor + Close() common.Executor } // NewContainer creates a reference to a container @@ -246,6 +247,16 @@ func (cr *containerReference) connect() common.Executor { } } +func (cr *containerReference) Close() common.Executor { + return func(ctx context.Context) error { + if cr.cli != nil { + cr.cli.Close() + cr.cli = nil + } + return nil + } +} + func (cr *containerReference) find() common.Executor { return func(ctx context.Context) error { if cr.id != "" { diff --git a/pkg/container/docker_volume.go b/pkg/container/docker_volume.go index 4a50261d..5a6d4764 100644 --- a/pkg/container/docker_volume.go +++ b/pkg/container/docker_volume.go @@ -13,6 +13,7 @@ func NewDockerVolumeRemoveExecutor(volume string, force bool) common.Executor { if err != nil { return err } + defer cli.Close() list, err := cli.VolumeList(ctx, filters.NewArgs()) if err != nil { @@ -43,6 +44,7 @@ func removeExecutor(volume string, force bool) common.Executor { if err != nil { return err } + defer cli.Close() return cli.VolumeRemove(ctx, volume, force) } diff --git a/pkg/runner/run_context.go b/pkg/runner/run_context.go index cb4c5c98..8494e261 100755 --- a/pkg/runner/run_context.go +++ b/pkg/runner/run_context.go @@ -231,7 +231,12 @@ func (rc *RunContext) Executor() common.Executor { } steps = append(steps, rc.stopJobContainer()) - return common.NewPipelineExecutor(steps...).If(rc.isEnabled) + return common.NewPipelineExecutor(steps...).Finally(func(ctx context.Context) error { + if rc.JobContainer != nil { + return rc.JobContainer.Close()(ctx) + } + return nil + }).If(rc.isEnabled) } func (rc *RunContext) newStepExecutor(step *model.Step) common.Executor { diff --git a/pkg/runner/step_context.go b/pkg/runner/step_context.go index 84ce43b8..dca156c7 100644 --- a/pkg/runner/step_context.go +++ b/pkg/runner/step_context.go @@ -332,7 +332,7 @@ func (sc *StepContext) runUsesContainer() common.Executor { stepContainer.Start(true), ).Finally( stepContainer.Remove().IfBool(!rc.Config.ReuseContainers), - )(ctx) + ).Finally(stepContainer.Close())(ctx) } } @@ -589,7 +589,7 @@ func (sc *StepContext) execAsDocker(ctx context.Context, action *model.Action, a stepContainer.Start(true), ).Finally( stepContainer.Remove().IfBool(!rc.Config.ReuseContainers), - )(ctx) + ).Finally(stepContainer.Close())(ctx) } func (sc *StepContext) execAsComposite(ctx context.Context, step *model.Step, _ string, rc *RunContext, containerActionDir string, actionName string, _ string, action *model.Action, maybeCopyToActionDir func() error) error {