samalba / dockerclient

Docker client library in Go
http://www.docker.com/
Apache License 2.0
322 stars 140 forks source link

Support for building images #62

Open advdv opened 9 years ago

advdv commented 9 years ago

Hey, I am working with this client after seeing it being used by Docker swarm and the client feels solid. I'm currently missing a way to build images though: https://docs.docker.com/reference/api/docker_remote_api_v1.16/#build-an-image-from-dockerfile-via-stdin Is this feature planned? Having looked around a bit it seems quite trivial to implement as long as we don't do any client trickery (.dockerignore etc), I can help out ofcourse.

divideandconquer commented 9 years ago

I too am looking for an easy way to do this.

advdv commented 9 years ago

I use the following right now (https://github.com/dockpit/pit/blob/master/client/docker.go#L146):

Basicly sending a post request with tar bytes in the request body, d.client is the samalba/dockerclient:

    // fall back to streaming ourselves for building the image
    // implement image building: https://github.com/samalba/dockerclient/issues/62
    req, err := http.NewRequest("POST", fmt.Sprintf("%s/build?t=%s", d.client.URL, iname), in)
    if err != nil {
        return "", errwrap.Wrapf(fmt.Sprintf("Failed to create Docker build request for '%s'::'%s': {{err}}", dep.Name, state.Name), err)
    }

    req.Header.Set("Content-Type", "application/tar")

    b.IsRunning = true
    resp, err := d.client.HTTPClient.Do(req)
    if err != nil {
        return "", errwrap.Wrapf(fmt.Sprintf("Docker build request failed for '%s'::'%s': {{err}}", dep.Name, state.Name), err)
    }

    b.IsRunning = false

    defer resp.Body.Close()
    defer io.Copy(out, resp.Body)
    if resp.StatusCode > 200 {
        return "", fmt.Errorf("Unexpected response while building Docker image for '%s'::'%s': %s", dep.Name, state.Name, resp.Status)
    }