0

I have the following code to build an image from a .tar file that contains a Dockerfile.

os.MkdirAll("configs/container",0755)
tar := new(archivex.TarFile)
tar.Create("configs/container/conf.tar")
tar.AddAll("configs/configdir", false)
tar.Close()

dockerBuildContext, err := os.Open("config/dns/conf.tar")
defer dockerBuildContext.Close()


buildOptions := 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", // optional, is the default
        Tags:   []string{"coolimage"},
    }

cli, err := client.NewEnvClient()

buildResponse, err := cli.ImageBuild(context.Background(), dockerBuildContext, buildOptions)
if err != nil {
        log.Printf("%s", err.Error())
}
if err != nil { 
    log.Fatal(err)
}
response, err := ioutil.ReadAll(buildResponse.Body) 
log.Printf(string(response))

defer buildResponse.Body.Close()

However, this is returning the following error:

2017/10/22 13:57:42 error during connect: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.34/build?buildargs=null&cachefrom=null&cgroupparent=cgroup_parent&cpuperiod=30&cpuquota=10&cpusetcpus=2&cpusetmems=12&cpushares=20&dockerfile=Dockerfile&labels=null&memory=256&memswap=512&networkmode=&rm=0&shmsize=10&t=dnsimage&target=&ulimits=null: invalid argument

I tried to remove all BuildOptions and only leave the "Dockerfile" option but same error still appears. I always get invalid argument.

I tried all ways showed on this question

I am running Docker on Mac but I also tried in an EC2 instance running CentOS and got the same result.

Client:
Version:         1.12.6
API version:     1.24
Package version: docker-1.12.6-55.gitc4618fb.el7.centos.x86_64
Go version:      go1.8.3
Git commit:      c4618fb/1.12.6
Built:           Thu Sep 21 22:33:52 2017
OS/Arch:         linux/amd64

Server:
Version:         1.12.6
API version:     1.24
Package version: docker-1.12.6-55.gitc4618fb.el7.centos.x86_64
Go version:      go1.8.3
Git commit:      c4618fb/1.12.6
Built:           Thu Sep 21 22:33:52 2017
OS/Arch:         linux/amd64

No errors appear on docker.log or journalctl I also tried with both github.com/docker/docker/client and github.com/moby/moby/client

3
  • Instead of build first try to run a basic command like getting container list. This seems to be an issue with connecting to docker daemon itself, rather than the build part. First figure out whether its a connection issue or a specific API parameter issue Oct 23, 2017 at 7:57
  • Thanks for your suggestion, I just tried that and other method like ImagePull and ContainerCreate and those are working ok. My problem seems to be isolated to ImageBuild
    – mkreder
    Oct 23, 2017 at 22:52
  • If you see the error, the issue seems you are hitting API version v1.34 while you should be hitting v1.24, try using func NewClient(host string, version string, client *http.Client, httpHeaders map[string]string) (*Client, error) by specifying version. Also check if you can use github.com/moby/moby/blob/… Oct 24, 2017 at 6:07

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.