From 00792d1704b0997dd769c60562209ef9c3ed71c4 Mon Sep 17 00:00:00 2001 From: Tibor Vass Date: Wed, 13 Jun 2018 18:25:22 +0000 Subject: [PATCH] build: ensure temporary folder is removed in error case Signed-off-by: Tibor Vass --- cli/command/image/build/context.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cli/command/image/build/context.go b/cli/command/image/build/context.go index ee94cd38a9..3e8076ee81 100644 --- a/cli/command/image/build/context.go +++ b/cli/command/image/build/context.go @@ -100,11 +100,16 @@ func DetectArchiveReader(input io.ReadCloser) (rc io.ReadCloser, isArchive bool, // WriteTempDockerfile writes a Dockerfile stream to a temporary file with a // name specified by DefaultDockerfileName and returns the path to the // temporary directory containing the Dockerfile. -func WriteTempDockerfile(rc io.ReadCloser) (string, error) { - dockerfileDir, err := ioutil.TempDir("", "docker-build-tempdockerfile-") +func WriteTempDockerfile(rc io.ReadCloser) (dockerfileDir string, err error) { + dockerfileDir, err = ioutil.TempDir("", "docker-build-tempdockerfile-") if err != nil { return "", errors.Errorf("unable to create temporary context directory: %v", err) } + defer func() { + if err != nil { + os.RemoveAll(dockerfileDir) + } + }() f, err := os.Create(filepath.Join(dockerfileDir, DefaultDockerfileName)) if err != nil {