(ctx context.Context, contextDir string, relDockerfile string)
| 76 | } |
| 77 | } |
| 78 | func createBuildContext(ctx context.Context, contextDir string, relDockerfile string) (io.ReadCloser, error) { |
| 79 | common.Logger(ctx).Debugf("Creating archive for build context dir '%s' with relative dockerfile '%s'", contextDir, relDockerfile) |
| 80 | |
| 81 | // And canonicalize dockerfile name to a platform-independent one |
| 82 | relDockerfile = filepath.ToSlash(relDockerfile) |
| 83 | |
| 84 | f, err := os.Open(filepath.Join(contextDir, ".dockerignore")) |
| 85 | if err != nil && !os.IsNotExist(err) { |
| 86 | return nil, err |
| 87 | } |
| 88 | defer f.Close() |
| 89 | |
| 90 | var excludes []string |
| 91 | if err == nil { |
| 92 | excludes, err = ignorefile.ReadAll(f) |
| 93 | if err != nil { |
| 94 | return nil, err |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // If .dockerignore mentions .dockerignore or the Dockerfile |
| 99 | // then make sure we send both files over to the daemon |
| 100 | // because Dockerfile is, obviously, needed no matter what, and |
| 101 | // .dockerignore is needed to know if either one needs to be |
| 102 | // removed. The daemon will remove them for us, if needed, after it |
| 103 | // parses the Dockerfile. Ignore errors here, as they will have been |
| 104 | // caught by validateContextDirectory above. |
| 105 | var includes = []string{"."} |
| 106 | keepThem1, _ := patternmatcher.Matches(".dockerignore", excludes) |
| 107 | keepThem2, _ := patternmatcher.Matches(relDockerfile, excludes) |
| 108 | if keepThem1 || keepThem2 { |
| 109 | includes = append(includes, ".dockerignore", relDockerfile) |
| 110 | } |
| 111 | |
| 112 | compression := archive.Uncompressed |
| 113 | buildCtx, err := archive.TarWithOptions(contextDir, &archive.TarOptions{ |
| 114 | Compression: compression, |
| 115 | ExcludePatterns: excludes, |
| 116 | IncludeFiles: includes, |
| 117 | }) |
| 118 | if err != nil { |
| 119 | return nil, err |
| 120 | } |
| 121 | |
| 122 | return buildCtx, nil |
| 123 | } |
no test coverage detected
searching dependent graphs…