@@ -382,17 +382,26 @@ func (ch *contextHash) hexSum() string {
382382}
383383
384384func 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+ }
388397 }
389398 ignorePatterns , err := dockerignore .ReadAll (bytes .NewReader (dockerIgnore ))
390399 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 )
392401 }
393402 ignoreMatcher , err := fileutils .NewPatternMatcher (ignorePatterns )
394403 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 )
396405 }
397406 ch := newContextHash (contextPath )
398407 err = ch .hashPath (dockerfile , 0 )
@@ -412,7 +421,7 @@ func hashContext(contextPath string, dockerfile string) (string, error) {
412421 }
413422 ignore , err := ignoreMatcher .Matches (path )
414423 if err != nil {
415- return fmt .Errorf (".dockerignore rule failed: %w" , err )
424+ return fmt .Errorf ("%s rule failed: %w" , dockerIgnorePath , err )
416425 }
417426 if ignore {
418427 if d .IsDir () {
0 commit comments