title: Build Docker Image From Go Code
body: <p>I'm trying to build a Docker image using the Docker API and Docker Go libraries (<a href="https://github.com/docker/engine-api/" rel="noreferrer">https://github.com/docker/engine-api/</a>). Code example:</p>
  
  <pre><code>package main
  import (
      "fmt"
      "github.com/docker/engine-api/client"
      "github.com/docker/engine-api/types"
      "golang.org/x/net/context"
  )
  func main() {
      defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
      cli, err := client.NewClient("unix:///var/run/docker.sock", "v1.22", nil, defaultHeaders)
      if err != nil {
          panic(err)
      }
      fmt.Print(cli.ClientVersion())
      opt := types.ImageBuildOptions{
          CPUSetCPUs:   "2",
          CPUSetMems:   "12",
          CPUShares:    20,
          CPUQuota:     10,
          CPUPeriod:    30,
          Memory:       256,
          MemorySwap:   512,
          ShmSize:      10,
          CgroupParent: "cgroup_parent",
          Dockerfile:   "dockerSrc/docker-debug-container/Dockerfile",
      }
      _, err = cli.ImageBuild(context.Background(), nil, opt)
      if err == nil || err.Error() != "Error response from daemon: Server error" {
          fmt.Printf("expected a Server Error, got %v", err)
      }
  }
  </code></pre>
  
  <p>The error is always same:</p>
  
  <blockquote>
    <p>Error response from daemon: Cannot locate specified Dockerfile: dockerSrc/docker-debug-container/Dockerfile </p>
  </blockquote>
  
  <p>or </p>
  
  <blockquote>
    <p>Error response from daemon: Cannot locate specified Dockerfile: Dockerfile</p>
  </blockquote>
  
  <p><strong>Things I've checked:</strong></p>
  
  <ol>
  <li>The folder exists in build path</li>
  <li>I tried both relative and absolute path</li>
  <li>There are no softlinks in the path </li>
  <li>I tried the same folder for binary and <code>Dockerfile</code></li>
  <li><code>docker build &lt;path&gt;</code> works</li>
  <li>and bunch of other stuff</li>
  </ol>
  
  <p>My other option was to use <code>RemoteContext</code> which looks like it works, but only for fully self contained dockerfiles, and not the ones with "local file presence".</p>
  
  <hr>
  
  <p><strong>Update:</strong>
  Tried passing <code>tar</code> as buffer, but got the same result with the following:</p>
  
  <pre><code>  dockerBuildContext, err := os.Open("&lt;path to&gt;/docker-debug-    container/docker-debug-container.tar")
    defer dockerBuildContext.Close()
  
      opt := types.ImageBuildOptions{
          Context:      dockerBuildContext,
          CPUSetCPUs:   "2",
          CPUSetMems:   "12",
          CPUShares:    20,
          CPUQuota:     10,
          CPUPeriod:    30,
          Memory:       256,
          MemorySwap:   512,
          ShmSize:      10,
          CgroupParent: "cgroup_parent",
          //  Dockerfile:   "Dockerfile",
      }
  
      _, err = cli.ImageBuild(context.Background(), nil, opt)
  </code></pre>