nolint:gocyclo
(ctx context.Context, dockerCli command.Cli, options buildOptions)
| 183 | |
| 184 | //nolint:gocyclo |
| 185 | func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions) error { |
| 186 | var ( |
| 187 | buildCtx io.ReadCloser |
| 188 | dockerfileCtx io.ReadCloser |
| 189 | contextDir string |
| 190 | relDockerfile string |
| 191 | progBuff io.Writer |
| 192 | buildBuff io.Writer |
| 193 | remote string |
| 194 | ) |
| 195 | |
| 196 | if options.platform != "" { |
| 197 | _, err := platforms.Parse(options.platform) |
| 198 | if err != nil { |
| 199 | return err |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | contextType, err := build.DetectContextType(options.context) |
| 204 | if err != nil { |
| 205 | return err |
| 206 | } |
| 207 | |
| 208 | if options.dockerfileFromStdin() { |
| 209 | if contextType == build.ContextTypeStdin { |
| 210 | return errors.New("invalid argument: can't use stdin for both build context and dockerfile") |
| 211 | } |
| 212 | dockerfileCtx = dockerCli.In() |
| 213 | } |
| 214 | |
| 215 | progBuff = dockerCli.Out() |
| 216 | buildBuff = dockerCli.Out() |
| 217 | if options.quiet { |
| 218 | progBuff = bytes.NewBuffer(nil) |
| 219 | buildBuff = bytes.NewBuffer(nil) |
| 220 | } |
| 221 | if options.imageIDFile != "" { |
| 222 | // Avoid leaving a stale file if we eventually fail |
| 223 | if err := os.Remove(options.imageIDFile); err != nil && !os.IsNotExist(err) { |
| 224 | return fmt.Errorf("removing image ID file: %w", err) |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | switch contextType { |
| 229 | case build.ContextTypeStdin: |
| 230 | // buildCtx is tar archive. if stdin was dockerfile then it is wrapped |
| 231 | buildCtx, relDockerfile, err = build.GetContextFromReader(dockerCli.In(), options.dockerfileName) |
| 232 | if err != nil { |
| 233 | return fmt.Errorf("unable to prepare context from STDIN: %w", err) |
| 234 | } |
| 235 | case build.ContextTypeLocal: |
| 236 | contextDir, relDockerfile, err = build.GetContextFromLocalDir(options.context, options.dockerfileName) |
| 237 | if err != nil { |
| 238 | return fmt.Errorf("unable to prepare context: %s", err) |
| 239 | } |
| 240 | if strings.HasPrefix(relDockerfile, ".."+string(filepath.Separator)) { |
| 241 | // Dockerfile is outside build-context; read the Dockerfile and pass it as dockerfileCtx |
| 242 | dockerfileCtx, err = os.Open(options.dockerfileName) |
searching dependent graphs…