From ebd544a302b66d7210eea69179ac829a7b2abd02 Mon Sep 17 00:00:00 2001 From: Timofey Kirillov Date: Wed, 1 Jun 2022 17:52:19 +0300 Subject: [PATCH] fix(dockerfile): support RUN with --mount from another stage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the following instruction used: RUN --mount=type=bind,from=build,source=/usr/local/test_project/dist,target=/usr/test_project/dist \ cp -v /usr/test_project/dist/prog.py /usr/local/bin/prog — change image digest when `from=build` stage digest has changed. Signed-off-by: Timofey Kirillov --- .../dockerfile_helpers/dockerfile_helpers.go | 22 ++++++++++++++----- pkg/build/stage/dockerfile.go | 14 +++++++++--- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/pkg/build/dockerfile_helpers/dockerfile_helpers.go b/pkg/build/dockerfile_helpers/dockerfile_helpers.go index 273167470d..740811aa2f 100644 --- a/pkg/build/dockerfile_helpers/dockerfile_helpers.go +++ b/pkg/build/dockerfile_helpers/dockerfile_helpers.go @@ -18,11 +18,23 @@ func ResolveDockerStagesFromValue(stages []instructions.Stage) { } for _, cmd := range s.Commands { - copyCmd, ok := cmd.(*instructions.CopyCommand) - if ok && copyCmd.From != "" { - from := strings.ToLower(copyCmd.From) - if val, ok := nameToIndex[from]; ok { - copyCmd.From = val + switch typedCmd := cmd.(type) { + case *instructions.CopyCommand: + if typedCmd.From != "" { + from := strings.ToLower(typedCmd.From) + if val, ok := nameToIndex[from]; ok { + typedCmd.From = val + } + } + + case *instructions.RunCommand: + for _, mount := range instructions.GetMounts(typedCmd) { + if mount.From != "" { + from := strings.ToLower(mount.From) + if val, ok := nameToIndex[from]; ok { + mount.From = val + } + } } } } diff --git a/pkg/build/stage/dockerfile.go b/pkg/build/stage/dockerfile.go index 3628993612..0449491388 100644 --- a/pkg/build/stage/dockerfile.go +++ b/pkg/build/stage/dockerfile.go @@ -474,12 +474,20 @@ func (s *DockerfileStage) GetDependencies(ctx context.Context, c Conveyor, _ con } for _, cmd := range stage.Commands { - copyCmd, ok := cmd.(*instructions.CopyCommand) - if ok && copyCmd.From != "" { - relatedStageIndex, err := strconv.Atoi(copyCmd.From) + switch typedCmd := cmd.(type) { + case *instructions.CopyCommand: + relatedStageIndex, err := strconv.Atoi(typedCmd.From) if err == nil && relatedStageIndex < len(stagesDependencies) { stagesDependencies[ind] = append(stagesDependencies[ind], stagesDependencies[relatedStageIndex]...) } + + case *instructions.RunCommand: + for _, mount := range instructions.GetMounts(typedCmd) { + relatedStageIndex, err := strconv.Atoi(mount.From) + if err == nil && relatedStageIndex < len(stagesDependencies) { + stagesDependencies[ind] = append(stagesDependencies[ind], stagesDependencies[relatedStageIndex]...) + } + } } } }