2016-10-03 160 views
1

我创造了围棋的一个项目,我同时使用“github.com/docker/docker/client”和“github.com/docker/docker/api/types”的时候,但当我尝试创建容器我得到以下错误:错误创建与golang搬运工引擎容器

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

我不明白为什么发生这种情况,并使用新golang泊坞窗引擎(旧的“github.com/docker/后只发生engine-api“现在已弃用)。

的代码没有什么复杂的,所以我不知道如果我失去了一些东西:

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

这就是所谓的initConfig执行以下操作:

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}} 

而且,这里是我的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

编辑

只是试图用一个简单的程序

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) 
     } 
} 

同dockerfile测试它,但我仍然得到同样的错误:

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

+0

看起来您的项目已启用https,但您的本地docker守护程序仍然处于纯http状态。也许这个文档有助于https://docs.docker.com/engine/security/https/ –

+0

你传递给你的docker实例的命令行选项是什么? ('PS英法| grep的docker'在'/等/泊坞窗/ daemon.json'你得到一个'卷曲--unix插座标准响应/var/run/docker.sock HTTP什么:/ V1? 0.23 /集装箱/ JSON?所有= 1' – Matt

回答

0
client.NewEnvClient() 

我最后一次尝试,这个API预计环境像DOCKER_HOST这样的变量的语法与普通的docker客户端不同。

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. 

要使用此,你需要DOCKER_HOST进行设置/导出这些格式之一:

  1. UNIX:/// var/run中/泊坞窗。袜子
  2. http://localhost:2375
  3. https://localhost:2376