@@ -258,21 +258,34 @@ func UserFromDockerfile(dockerfileContent string) string {
258
258
// ImageFromDockerfile inspects the contents of a provided Dockerfile
259
259
// and returns the image that will be used to run the container.
260
260
func ImageFromDockerfile (dockerfileContent string ) (name.Reference , error ) {
261
+ args := map [string ]string {}
262
+ var imageRef string
261
263
lines := strings .Split (dockerfileContent , "\n " )
262
264
// Iterate over lines in reverse
263
265
for i := len (lines ) - 1 ; i >= 0 ; i -- {
264
266
line := lines [i ]
265
- if ! strings .HasPrefix (line , "FROM " ) {
267
+ if strings .HasPrefix (line , "ARG " ) {
268
+ arg := strings .TrimSpace (strings .TrimPrefix (line , "ARG " ))
269
+ if strings .Contains (arg , "=" ) {
270
+ parts := strings .SplitN (arg , "=" , 2 )
271
+ args [parts [0 ]] = parts [1 ]
272
+ }
266
273
continue
267
274
}
268
- imageRef := strings .TrimSpace (strings .TrimPrefix (line , "FROM " ))
269
- image , err := name .ParseReference (imageRef )
270
- if err != nil {
271
- return nil , fmt .Errorf ("parse image ref %q: %w" , imageRef , err )
275
+ if imageRef == "" && strings .HasPrefix (line , "FROM " ) {
276
+ imageRef = strings .TrimPrefix (line , "FROM " )
272
277
}
273
- return image , nil
274
278
}
275
- return nil , fmt .Errorf ("no FROM directive found" )
279
+ if imageRef == "" {
280
+ return nil , fmt .Errorf ("no FROM directive found" )
281
+ }
282
+ image , err := name .ParseReference (os .Expand (imageRef , func (s string ) string {
283
+ return args [s ]
284
+ }))
285
+ if err != nil {
286
+ return nil , fmt .Errorf ("parse image ref %q: %w" , imageRef , err )
287
+ }
288
+ return image , nil
276
289
}
277
290
278
291
// UserFromImage inspects the remote reference and returns the user
0 commit comments