Skip to content

Commit c0ea3dd

Browse files
committed
Implement parsing for RUN --mount=type=bind,from=...
1 parent d61579c commit c0ea3dd

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

pkg/dockerfile/parse.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,48 @@ func ParseReader(dockerfile io.Reader) (*Metadata, error) {
105105
// make sure to add ":latest" if it's implied
106106
from = latestizeRepoTag(from)
107107

108+
meta.Froms = append(meta.Froms, from)
109+
}
110+
case "RUN": // TODO combine this and the above COPY-parsing code somehow sanely
111+
for _, arg := range fields[1:] {
112+
if !strings.HasPrefix(arg, "--") {
113+
// doesn't appear to be a "flag"; time to bail!
114+
break
115+
}
116+
if !strings.HasPrefix(arg, "--mount=") {
117+
// ignore any flags we're not interested in
118+
continue
119+
}
120+
csv := arg[len("--mount="):]
121+
// TODO more correct CSV parsing
122+
fields := strings.Split(csv, ",")
123+
var mountType, from string
124+
for _, field := range fields {
125+
if strings.HasPrefix(field, "type=") {
126+
mountType = field[len("type="):]
127+
continue
128+
}
129+
if strings.HasPrefix(field, "from=") {
130+
from = field[len("from="):]
131+
continue
132+
}
133+
}
134+
if mountType != "bind" || from == "" {
135+
// this is probably something we should be worried about, but not something we're interested in parsing
136+
continue
137+
}
138+
139+
if stageFrom, ok := meta.StageNameFroms[from]; ok {
140+
// see note above regarding stage names in FROM
141+
from = stageFrom
142+
} else if stageNumber, err := strconv.Atoi(from); err == nil && stageNumber < len(meta.StageFroms) {
143+
// must be a stage number, we should resolve it too
144+
from = meta.StageFroms[stageNumber]
145+
}
146+
147+
// make sure to add ":latest" if it's implied
148+
from = latestizeRepoTag(from)
149+
108150
meta.Froms = append(meta.Froms, from)
109151
}
110152
}

pkg/dockerfile/parse_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestParse(t *testing.T) {
8181
`,
8282
metadata: dockerfile.Metadata{
8383
StageFroms: []string{"scratch"},
84-
Froms: []string{"scratch"}, // TODO this should include "busybox:uclibc"
84+
Froms: []string{"scratch", "busybox:uclibc"},
8585
},
8686
},
8787
{
@@ -95,7 +95,7 @@ func TestParse(t *testing.T) {
9595
StageFroms: []string{"busybox:uclibc", "scratch"},
9696
StageNames: []string{"bb"},
9797
StageNameFroms: map[string]string{"bb": "busybox:uclibc"},
98-
Froms: []string{"busybox:uclibc", "scratch"}, // TODO this should end with "busybox:uclibc"
98+
Froms: []string{"busybox:uclibc", "scratch", "busybox:uclibc"},
9999
},
100100
},
101101
} {

0 commit comments

Comments
 (0)