Skip to content

Commit c4692fa

Browse files
committed
Implement parsing for RUN --mount=type=bind,from=...
1 parent 2fd5489 commit c4692fa

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

pkg/dockerfile/parse.go

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

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

pkg/dockerfile/parse_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func TestParse(t *testing.T) {
101101
`,
102102
metadata: dockerfile.Metadata{
103103
StageFroms: []string{"scratch"},
104-
Froms: []string{"scratch"}, // TODO this should include "busybox:uclibc"
104+
Froms: []string{"scratch", "busybox:uclibc"},
105105
},
106106
},
107107
{
@@ -116,7 +116,7 @@ func TestParse(t *testing.T) {
116116
StageFroms: []string{"busybox:uclibc", "scratch"},
117117
StageNames: []string{"bb"},
118118
StageNameFroms: map[string]string{"bb": "busybox:uclibc"},
119-
Froms: []string{"busybox:uclibc", "scratch"}, // TODO this should end with "busybox:uclibc"
119+
Froms: []string{"busybox:uclibc", "scratch", "busybox:uclibc"},
120120
},
121121
},
122122
} {

0 commit comments

Comments
 (0)