@@ -277,16 +277,16 @@ func tryReadIgnoreFile(cwd, ignorefile string) io.ReadCloser {
277
277
// writeDefaultIgnoreFile writes a default
278
278
// .dockerignore file to the specified directory.
279
279
// Returns the filename of the written file and an error.
280
- func writeDefaultIgnoreFile (cwd string ) (string , error ) {
281
- path := filepath .Join (cwd , dotdockerignore )
280
+ func writeDefaultIgnoreFile (cwd string , dockerignore string ) (string , error ) {
281
+ path := filepath .Join (cwd , dockerignore )
282
282
term .Debug ("Writing .dockerignore file to" , path )
283
283
284
284
err := os .WriteFile (path , []byte (defaultDockerIgnore ), 0644 )
285
285
if err != nil {
286
286
return "" , fmt .Errorf ("failed to write default .dockerignore file: %w" , err )
287
287
}
288
288
289
- return dotdockerignore , nil
289
+ return dockerignore , nil
290
290
}
291
291
292
292
// getDockerIgnorePatterns attempts to read the ignore file
@@ -302,18 +302,9 @@ func getDockerIgnorePatterns(root, dockerfile string) ([]string, string, error)
302
302
dockerignore = dotdockerignore
303
303
reader = tryReadIgnoreFile (root , dockerignore )
304
304
if reader == nil {
305
- // Generate a default .dockerignore file if none exists
306
- term .Warn ("No .dockerignore file found; generating default .dockerignore" )
307
- var err error
308
- dockerignore , err = writeDefaultIgnoreFile (root )
309
- if err != nil {
310
- return nil , "" , fmt .Errorf ("failed to write default .dockerignore file: %w" , err )
311
- }
312
- // Try reading the newly created .dockerignore file
313
- reader = tryReadIgnoreFile (root , dockerignore )
314
- if reader == nil {
315
- return nil , "" , fmt .Errorf ("failed to read default .dockerignore file: %s at the path: %s" , dockerignore , root )
316
- }
305
+ // No .dockerignore file found; read from defaults
306
+ dockerignore = ""
307
+ reader = io .NopCloser (strings .NewReader (defaultDockerIgnore ))
317
308
}
318
309
}
319
310
@@ -328,6 +319,15 @@ func getDockerIgnorePatterns(root, dockerfile string) ([]string, string, error)
328
319
}
329
320
330
321
func WalkContextFolder (root , dockerfile string , fn func (path string , de os.DirEntry , slashPath string ) error ) error {
322
+ return walkContextFolder (root , dockerfile , writeIgnoreFileNo , fn )
323
+ }
324
+
325
+ type writeIgnoreFile bool
326
+
327
+ const writeIgnoreFileNo writeIgnoreFile = false
328
+ const writeIgnoreFileYes writeIgnoreFile = true
329
+
330
+ func walkContextFolder (root , dockerfile string , writeIgnore writeIgnoreFile , fn func (path string , de os.DirEntry , slashPath string ) error ) error {
331
331
if dockerfile == "" {
332
332
dockerfile = "Dockerfile"
333
333
} else {
@@ -340,6 +340,16 @@ func WalkContextFolder(root, dockerfile string, fn func(path string, de os.DirEn
340
340
return err
341
341
}
342
342
343
+ if dockerignore == "" && writeIgnore {
344
+ // Generate a default .dockerignore file if none exists (to be included in the context)
345
+ term .Warn ("No .dockerignore file found; creating default .dockerignore" )
346
+ var err error
347
+ dockerignore , err = writeDefaultIgnoreFile (root , dotdockerignore )
348
+ if err != nil {
349
+ return fmt .Errorf ("failed to write default .dockerignore file: %w" , err )
350
+ }
351
+ }
352
+
343
353
pm , err := patternmatcher .New (patterns )
344
354
if err != nil {
345
355
return err
@@ -363,11 +373,12 @@ func WalkContextFolder(root, dockerfile string, fn func(path string, de os.DirEn
363
373
364
374
slashPath := filepath .ToSlash (relPath )
365
375
366
- if relPath == dockerfile {
376
+ switch relPath {
377
+ case dockerfile :
367
378
// we need the Dockerfile, even if it's in the .dockerignore file
368
- } else if relPath == dockerignore {
379
+ case dockerignore :
369
380
// we need the .dockerignore file too: it might ignore itself and/or the Dockerfile, but is needed by the builder
370
- } else {
381
+ default :
371
382
// Ignore files using the dockerignore patternmatcher
372
383
ignore , err := pm .MatchesOrParentMatches (slashPath ) // always use forward slashes
373
384
if err != nil {
@@ -407,7 +418,7 @@ func createArchive(ctx context.Context, root string, dockerfile string, contentT
407
418
}
408
419
409
420
doProgress := term .StdoutCanColor () && term .IsTerminal ()
410
- err := WalkContextFolder (root , dockerfile , func (path string , de os.DirEntry , slashPath string ) error {
421
+ err := walkContextFolder (root , dockerfile , writeIgnoreFileYes , func (path string , de os.DirEntry , slashPath string ) error {
411
422
if term .DoDebug () {
412
423
term .Debug ("Adding" , slashPath )
413
424
} else if doProgress {
0 commit comments