From 5e275d982472e8e5bef15a1f4b9ab4881a491849 Mon Sep 17 00:00:00 2001 From: Tejal Desai Date: Tue, 5 May 2020 21:27:28 -0700 Subject: [PATCH 1/2] Apply dockefile exclude only for first stage --- integration/dockerfiles/Dockerfile_test_dockerignore | 8 +++++++- pkg/executor/build.go | 2 ++ pkg/util/fs_util.go | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/integration/dockerfiles/Dockerfile_test_dockerignore b/integration/dockerfiles/Dockerfile_test_dockerignore index d646872ef..70667623d 100644 --- a/integration/dockerfiles/Dockerfile_test_dockerignore +++ b/integration/dockerfiles/Dockerfile_test_dockerignore @@ -1,5 +1,11 @@ # This dockerfile makes sure the .dockerignore is working # If so then ignore/foo should copy to /foo # If not, then this image won't build because it will attempt to copy three files to /foo, which is a file not a directory -FROM scratch +FROM scratch as base COPY ignore/* /foo + +From base as first +COPY --from=base /foo ignore/bar + +FROM first +COPY --from=first ignore/* /fooAnother/ \ No newline at end of file diff --git a/pkg/executor/build.go b/pkg/executor/build.go index 87a44df43..ea8bf8107 100644 --- a/pkg/executor/build.go +++ b/pkg/executor/build.go @@ -596,6 +596,7 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) { } logrus.Infof("Built cross stage deps: %v", crossStageDependencies) + util.IsFirstStage = true for index, stage := range kanikoStages { sb, err := newStageBuilder(opts, stage, crossStageDependencies, digestToCacheKey, stageIdxToDigest, stageNameToIdx) if err != nil { @@ -604,6 +605,7 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) { if err := sb.build(); err != nil { return nil, errors.Wrap(err, "error building stage") } + util.IsFirstStage = false reviewConfig(stage, &sb.cf.Config) diff --git a/pkg/util/fs_util.go b/pkg/util/fs_util.go index 230d87669..714408656 100644 --- a/pkg/util/fs_util.go +++ b/pkg/util/fs_util.go @@ -73,6 +73,7 @@ var whitelist = initialWhitelist var volumes = []string{} var excluded []string +var IsFirstStage = true type ExtractFunction func(string, *tar.Header, io.Reader) error @@ -675,6 +676,10 @@ func GetExcludedFiles(dockerfilepath string, buildcontext string) error { // ExcludeFile returns true if the .dockerignore specified this file should be ignored func ExcludeFile(path, buildcontext string) bool { + // Do not apply dockerfile excludes for first stage + if !IsFirstStage { + return false + } if HasFilepathPrefix(path, buildcontext, false) { var err error path, err = filepath.Rel(buildcontext, path) From 113c2397ea367d8f09330a5ed103b4d996c6a9d8 Mon Sep 17 00:00:00 2001 From: Tejal Desai Date: Tue, 5 May 2020 22:27:28 -0700 Subject: [PATCH 2/2] Update comment. --- pkg/util/fs_util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/util/fs_util.go b/pkg/util/fs_util.go index 714408656..2b44f97ce 100644 --- a/pkg/util/fs_util.go +++ b/pkg/util/fs_util.go @@ -676,7 +676,7 @@ func GetExcludedFiles(dockerfilepath string, buildcontext string) error { // ExcludeFile returns true if the .dockerignore specified this file should be ignored func ExcludeFile(path, buildcontext string) bool { - // Do not apply dockerfile excludes for first stage + // Apply dockerfile excludes for first stage only if !IsFirstStage { return false }