@@ -382,17 +382,26 @@ func (ch *contextHash) hexSum() string {
382
382
}
383
383
384
384
func hashContext (contextPath string , dockerfile string ) (string , error ) {
385
- dockerIgnore , err := os .ReadFile (filepath .Join (contextPath , ".dockerignore" ))
386
- if err != nil && ! os .IsNotExist (err ) {
387
- return "" , fmt .Errorf ("unable to read .dockerignore file: %w" , err )
385
+ dockerIgnorePath := dockerfile + ".dockerignore"
386
+ dockerIgnore , err := os .ReadFile (dockerIgnorePath )
387
+ if err != nil {
388
+ if os .IsNotExist (err ) {
389
+ dockerIgnorePath = filepath .Join (contextPath , ".dockerignore" )
390
+ dockerIgnore , err = os .ReadFile (dockerIgnorePath )
391
+ if err != nil && ! os .IsNotExist (err ) {
392
+ return "" , fmt .Errorf ("unable to read %s file: %w" , dockerIgnorePath , err )
393
+ }
394
+ } else {
395
+ return "" , fmt .Errorf ("unable to read %s file: %w" , dockerIgnorePath , err )
396
+ }
388
397
}
389
398
ignorePatterns , err := dockerignore .ReadAll (bytes .NewReader (dockerIgnore ))
390
399
if err != nil {
391
- return "" , fmt .Errorf ("unable to parse .dockerignore file: %w" , err )
400
+ return "" , fmt .Errorf ("unable to parse %s file: %w" , dockerIgnorePath , err )
392
401
}
393
402
ignoreMatcher , err := fileutils .NewPatternMatcher (ignorePatterns )
394
403
if err != nil {
395
- return "" , fmt .Errorf ("unable to load rules from .dockerignore : %w" , err )
404
+ return "" , fmt .Errorf ("unable to load rules from %s : %w" , dockerIgnorePath , err )
396
405
}
397
406
ch := newContextHash (contextPath )
398
407
err = ch .hashPath (dockerfile , 0 )
@@ -412,7 +421,7 @@ func hashContext(contextPath string, dockerfile string) (string, error) {
412
421
}
413
422
ignore , err := ignoreMatcher .Matches (path )
414
423
if err != nil {
415
- return fmt .Errorf (".dockerignore rule failed: %w" , err )
424
+ return fmt .Errorf ("%s rule failed: %w" , dockerIgnorePath , err )
416
425
}
417
426
if ignore {
418
427
if d .IsDir () {
0 commit comments