Skip to content

Commit

Permalink
Merge pull request #26 from mkorolyov/remove-image-when-container-stops
Browse files Browse the repository at this point in the history
remove image to save space and cleanup when container stopped after the execution
  • Loading branch information
dhui committed Mar 26, 2024
2 parents 4eca1c5 + 1638e2b commit 0b6d55a
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions dktest.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func pullImage(ctx context.Context, lgr Logger, dc client.ImageAPIClient, imgNam
return nil
}

func removeImage(ctx context.Context, lgr Logger, dc client.ImageAPIClient, imgName string) {
lgr.Log("Removing image:", imgName)

if _, err := dc.ImageRemove(ctx, imgName, types.ImageRemoveOptions{Force: true, PruneChildren: true}); err != nil {
lgr.Log("Failed to remove image: ", err.Error())
}
}

func runImage(ctx context.Context, lgr Logger, dc client.ContainerAPIClient, imgName string,
opts Options) (ContainerInfo, error) {
c := ContainerInfo{Name: genContainerName(), ImageName: imgName}
Expand Down Expand Up @@ -209,6 +217,7 @@ func RunContext(ctx context.Context, logger Logger, imgName string, opts Options
stopCtx, stopTimeoutCancelFunc := context.WithTimeout(ctx, opts.CleanupTimeout)
defer stopTimeoutCancelFunc()
stopContainer(stopCtx, logger, dc, c, opts.LogStdout, opts.LogStderr)
removeImage(ctx, logger, dc, imgName)
}()

if waitContainerReady(runCtx, logger, c, opts.ReadyFunc, opts.ReadyTimeout) {
Expand Down

0 comments on commit 0b6d55a

Please sign in to comment.