From d86fb3b9959cd62641c2d06c3fa92d9c017ca203 Mon Sep 17 00:00:00 2001 From: zulkhair Date: Thu, 8 Aug 2024 11:01:54 +0700 Subject: [PATCH] move tar writer close --- common/docker/docker.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/common/docker/docker.go b/common/docker/docker.go index 1f880ba..e15d5e7 100644 --- a/common/docker/docker.go +++ b/common/docker/docker.go @@ -291,9 +291,12 @@ func pullImageIfNotExists(ctx context.Context, cli *client.Client, imageName str func buildImage(ctx context.Context, cli *client.Client, dockerfile Dockerfile, imageName string) error { fmt.Println("Building image ", imageName) + + // Enable BuildKit via environment variable + os.Setenv("DOCKER_BUILDKIT", "1") + buf := new(bytes.Buffer) tw := tar.NewWriter(buf) - defer tw.Close() // Add the Dockerfile to the tar archive header := &tar.Header{ @@ -312,8 +315,13 @@ func buildImage(ctx context.Context, cli *client.Client, dockerfile Dockerfile, return err } + if err := tw.Close(); err != nil { + return eris.Wrap(err, "Failed to close tar writer") + } + // Read the tar archive - tarReader := bytes.NewReader(buf.Bytes()) + buildContext := io.NopCloser(bytes.NewReader(buf.Bytes())) + defer buildContext.Close() buildOptions := types.ImageBuildOptions{ Dockerfile: "Dockerfile", @@ -323,7 +331,7 @@ func buildImage(ctx context.Context, cli *client.Client, dockerfile Dockerfile, } // Build the image - buildResponse, err := cli.ImageBuild(ctx, tarReader, buildOptions) + buildResponse, err := cli.ImageBuild(ctx, buildContext, buildOptions) if err != nil { return err }