Skip to content

Commit

Permalink
Implement parsing for RUN --mount=type=bind,from=...
Browse files Browse the repository at this point in the history
  • Loading branch information
tianon committed Jan 9, 2025
1 parent b8fae83 commit f6b7642
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
43 changes: 43 additions & 0 deletions pkg/dockerfile/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,49 @@ func ParseReader(dockerfile io.Reader) (*Metadata, error) {
// make sure to add ":latest" if it's implied
from = latestizeRepoTag(from)

meta.Froms = append(meta.Froms, from)
}

case "RUN": // TODO combine this and the above COPY-parsing code somehow sanely
for _, arg := range fields[1:] {
if !strings.HasPrefix(arg, "--") {
// doesn't appear to be a "flag"; time to bail!
break
}
if !strings.HasPrefix(arg, "--mount=") {
// ignore any flags we're not interested in
continue
}
csv := arg[len("--mount="):]
// TODO more correct CSV parsing
fields := strings.Split(csv, ",")
var mountType, from string
for _, field := range fields {
if strings.HasPrefix(field, "type=") {
mountType = field[len("type="):]
continue
}
if strings.HasPrefix(field, "from=") {
from = field[len("from="):]
continue
}
}
if mountType != "bind" || from == "" {
// this is probably something we should be worried about, but not something we're interested in parsing
continue
}

if stageFrom, ok := meta.StageNameFroms[from]; ok {
// see note above regarding stage names in FROM
from = stageFrom
} else if stageNumber, err := strconv.Atoi(from); err == nil && stageNumber < len(meta.StageFroms) {
// must be a stage number, we should resolve it too
from = meta.StageFroms[stageNumber]
}

// make sure to add ":latest" if it's implied
from = latestizeRepoTag(from)

meta.Froms = append(meta.Froms, from)
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/dockerfile/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestParse(t *testing.T) {
`,
metadata: dockerfile.Metadata{
StageFroms: []string{"scratch"},
Froms: []string{"scratch"}, // TODO this should include "busybox:uclibc"
Froms: []string{"scratch", "busybox:uclibc"},
},
},
{
Expand All @@ -98,7 +98,7 @@ func TestParse(t *testing.T) {
StageFroms: []string{"busybox:uclibc", "scratch"},
StageNames: []string{"bb"},
StageNameFroms: map[string]string{"bb": "busybox:uclibc"},
Froms: []string{"busybox:uclibc", "scratch"}, // TODO this should end with "busybox:uclibc"
Froms: []string{"busybox:uclibc", "scratch", "busybox:uclibc"},
},
},
} {
Expand Down

0 comments on commit f6b7642

Please sign in to comment.