dongshi6529 2016-10-03 20:52
浏览 80

使用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

  • 写回答

1条回答 默认 最新

  • dongyun9120 2017-04-13 00:33
    关注
    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
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算