From 6d7ab38f33edb9ab87a290a0c68cfd27b55b061f Mon Sep 17 00:00:00 2001 From: Nalin Dahyabhai Date: Wed, 8 Jan 2020 11:02:05 -0500 Subject: [PATCH 1/2] Check for .dockerignore specifically When generating the list of exclusions to process .dockerignore contents, don't include .dockerignore if we don't have a .dockerignore file in the context directory. That way, if the file doesn't exist, and the caller didn't pass in any patterns, we get no patterns instead of just one ".dockerignore" pattern, and we can hit the faster copy path. Signed-off-by: Nalin Dahyabhai Closes: #2072 Approved by: giuseppe --- add.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/add.go b/add.go index b5119e369..e82a5ef9a 100644 --- a/add.go +++ b/add.go @@ -215,7 +215,12 @@ func dockerIgnoreMatcher(lines []string, contextDir string) (*fileutils.PatternM if contextDir == "" { return nil, nil } - patterns := []string{".dockerignore"} + // If there's no .dockerignore file, then we don't have to add a + // pattern to tell copy logic to ignore it later. + var patterns []string + if _, err := os.Stat(filepath.Join(contextDir, ".dockerignore")); err == nil || !os.IsNotExist(err) { + patterns = []string{".dockerignore"} + } for _, ignoreSpec := range lines { ignoreSpec = strings.TrimSpace(ignoreSpec) // ignore comments passed back from .dockerignore @@ -224,7 +229,8 @@ func dockerIgnoreMatcher(lines []string, contextDir string) (*fileutils.PatternM } // if the spec starts with '!' it means the pattern // should be included. make a note so that we can move - // it to the front of the updated pattern + // it to the front of the updated pattern, and insert + // the context dir's path in between includeFlag := "" if strings.HasPrefix(ignoreSpec, "!") { includeFlag = "!" From f999964084ce75c833b0cffd17fb09b947dad506 Mon Sep 17 00:00:00 2001 From: Nalin Dahyabhai Date: Wed, 8 Jan 2020 11:04:57 -0500 Subject: [PATCH 2/2] copyFileWithTar: close source files at the right time Close source files after we've finished reading from them, rather than leaving it for later. Signed-off-by: Nalin Dahyabhai Closes: #2072 Approved by: giuseppe --- util.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/util.go b/util.go index b4670e41c..2f923357c 100644 --- a/util.go +++ b/util.go @@ -165,11 +165,6 @@ func (b *Builder) copyFileWithTar(tarIDMappingOptions *IDMappingOptions, chownOp if err != nil { return errors.Wrapf(err, "error opening %q to copy its contents", src) } - defer func() { - if err := f.Close(); err != nil { - logrus.Debugf("error closing %s: %v", fi.Name(), err) - } - }() } } @@ -200,6 +195,9 @@ func (b *Builder) copyFileWithTar(tarIDMappingOptions *IDMappingOptions, chownOp logrus.Debugf("error copying contents of %s: %v", fi.Name(), err) copyErr = err } + if err = srcFile.Close(); err != nil { + logrus.Debugf("error closing %s: %v", fi.Name(), err) + } } if err = writer.Close(); err != nil { logrus.Debugf("error closing write pipe for %s: %v", hdr.Name, err) @@ -213,7 +211,6 @@ func (b *Builder) copyFileWithTar(tarIDMappingOptions *IDMappingOptions, chownOp if err == nil { err = copyErr } - f = nil if pipeWriter != nil { pipeWriter.Close() }