func BuildImage(buildContext io.Reader, image string) error {
    ctx := context.Background()
    cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
    if err != nil {
        return err
    }
    defer cli.Close()

    resp, err := cli.ImageBuild(ctx, buildContext, types.ImageBuildOptions{Tags: []string{image}})
    if err != nil {
        return err
    }
    defer resp.Body.Close()

    // Possible that cli.ImageBuild() does not return an error, but we see an error from the response body
    scanner := bufio.NewScanner(resp.Body)
    for scanner.Scan() {
        line := scanner.Text()
        // fmt.Println(line)

        var respObject struct {
            // Most response lines have "stream" field instead of "error"
            // But we only care about "error" field
            Error string
        }

        err = json.Unmarshal([]byte(line), &respObject)
        if err != nil {
            return err
        }

        if respObject.Error != "" {
            return errors.New(respObject.Error)
        }
    }

    return nil
}
// Builds an image given a build context and image name
// buildContext is a tar archive containing all files needed to build image, including Dockerfile
Builds an image given a build context and image name
buildContext is a tar archive containing all files needed to build image, including Dockerfile
// Builds an image given a build context and image name // buildContext is a tar archive containing all files needed to build image including Dockerfile
func BuildImage(buildContext io.Reader, image string) error {
    ctx := context.Background()
    cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
    if err != nil {
        return err
    }
    defer cli.Close()
    resp, err := cli.ImageBuild(ctx, buildContext, types.ImageBuildOptions{Tags: []string{image}})
    if err != nil {
        return err
    }
    defer resp.Body.Close()
    scanner := bufio.NewScanner(resp.Body)
    for scanner.Scan() {
        line := scanner.Text()
        var respObject struct {
            Error string
        }
        err = json.Unmarshal([]byte(line), &respObject)
        if err != nil {
            return err
        }
        if respObject.Error != "" {
            return errors.New(respObject.Error)
        }
    }
    return nil
}
func BuildImage ( buildContext io . Reader , image string ) error { ctx := context . Background ( ) cli , err := client . NewClientWithOpts ( client . FromEnv , client . WithAPIVersionNegotiation ( ) ) if err != nil { return err } defer cli . Close ( ) resp , err := cli . ImageBuild ( ctx , buildContext , types . ImageBuildOptions { Tags : [ ] string { image } } ) if err != nil { return err } defer resp . Body . Close ( ) scanner := bufio . NewScanner ( resp . Body ) for scanner . Scan ( ) { line := scanner . Text ( ) var respObject struct { Error string } err = json . Unmarshal ( [ ] byte ( line ) , & respObject ) if err != nil { return err } if respObject . Error != " " { return errors . New ( respObject . Error ) } } return nil }
Builds an image given a build context and image name
buildContext is a tar archive containing all files needed to build image, including Dockerfile
Builds an image given a build context and image name buildContext is a tar archive containing all files needed to build image including Dockerfile
// Possible that cli.ImageBuild() does not return an error, but we see an error from the response body // fmt.Println(line) // Most response lines have "stream" field instead of "error" // But we only care about "error" field