Skip to content
This repository was archived by the owner on Mar 11, 2023. It is now read-only.

Commit 3ce16b8

Browse files
Support per-Dockerfile dockerignore files
Since Docker 19.03, Docker looks for ${dockerfile_name}.dockerignore files first, before falling back to .dockerignore if it doesn't exist.
1 parent 4783008 commit 3ce16b8

File tree

1 file changed

+15
-6
lines changed
  • cmd/pulumi-resource-docker-buildkit

1 file changed

+15
-6
lines changed

cmd/pulumi-resource-docker-buildkit/main.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,17 +382,26 @@ func (ch *contextHash) hexSum() string {
382382
}
383383

384384
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+
}
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

Comments
 (0)