使用Golang Docker引擎创建容器时出错

I am creating a project in Go and I am using both "github.com/docker/docker/client" and "github.com/docker/docker/api/types", but when I try and create a container I get the following error:

ERROR: 2016/10/03 22:39:26 containers.go:84: error during connect: Post https://%2Fvar%2Frun%2Fdocker.sock/v1.23/containers/create: http: server gave HTTP response to HTTPS client

I can't understand why this is happening and it only happened after using the new golang docker engine(the old "github.com/docker/engine-api" is now deprecated).

The code isn't anything complicated, so I wonder if I am missing something:

    resp, err := cli.Pcli.ContainerCreate(context.Background(), initConfig(), nil, nil, "")
    if err != nil {
            return err
    }

And the initConfig that is called does the following:

func initConfig() (config *container.Config) {
    mount := map[string]struct{}{"/root/host": {}}
    return &container.Config{Image: "leadis_image", Volumes: mount, Cmd: strslice.StrSlice{"/root/server.py"}, AttachStdout: true}}

Also here is my dockerfile

FROM debian

MAINTAINER Leadis Journey

LABEL Description="This docker image is used to compile and execute user's program."

LABEL Version="0.1"

VOLUME /root/host/

RUN apt-get update && yes | apt-get upgrade

RUN yes | apt-get install gcc g++ python3 make

COPY container.py /root/server.py

EDIT

Just tried to test it with a simpler program

package main

import (
        "fmt"
        "os"
        "io/ioutil"
        "github.com/docker/docker/client"
        "github.com/docker/docker/api/types"
        "github.com/docker/docker/api/types/container"
        "github.com/docker/docker/api/types/strslice"
        "golang.org/x/net/context"
)


func initConfig() (config *container.Config) {
        mount := map[string]struct{}{"/root/host": {}}
        return &container.Config{Image: "leadis_image", Volumes: mount, Cmd: strslice.StrSlice{"/root/server.py"}, AttachStdout: true}
}

func main() {
        client, _ := client.NewEnvClient()


        cwd, _ := os.Getwd()
        ctx, err := os.Open(cwd+"/Dockerfile.tar.gz")
        if err != nil {
                fmt.Println(err)
                return
        }
        build, err := client.ImageBuild(context.Background(), ctx, types.ImageBuildOptions{Tags: []string{"leadis_image"}, Context: ctx, SuppressOutput: false})
        if err != nil {
                fmt.Println(err)
                return
        }

        b, _ := ioutil.ReadAll(build.Body)
        fmt.Println(string(b))
        _, err = client.ContainerCreate(context.Background(), initConfig(), nil, nil, "")
        if err != nil {
                fmt.Println(err)
        }
}

Same dockerfile, but I still get the same error:

error during connect: Post https://%2Fvar%2Frun%2Fdocker.sock/v1.23/containers/create: http: server gave HTTP response to HTTPS client

client.NewEnvClient()

Last time I tried, this API expects environment variables like DOCKER_HOST in a different syntax from than the normal docker client.

From the client.go:

// NewEnvClient initializes a new API client based on environment variables.
// Use DOCKER_HOST to set the url to the docker server.
// Use DOCKER_API_VERSION to set the version of the API to reach, leave empty for latest.
// Use DOCKER_CERT_PATH to load the TLS certificates from.
// Use DOCKER_TLS_VERIFY to enable or disable TLS verification, off by default.

To use this, you need DOCKER_HOST to be set/exported in one of these formats:

  1. unix:///var/run/docker.sock
  2. http://localhost:2375
  3. https://localhost:2376