【问题标题】:Error when creating container with golang docker engine使用 golang docker 引擎创建容器时出错
【发布时间】:2017-04-13 08:33:44
【问题描述】:

我在 Go 中创建一个项目,我同时使用“github.com/docker/docker/client”和“github.com/docker/docker/api/types”,但是当我尝试创建一个容器时,我得到以下错误:

ERROR: 2016/10/03 22:39:26 containers.go:84: error during connect: Post https://%2Fvar%2Frun%2Fdocker.sock/v1.23/containers/create: http: 服务器向 HTTPS 客户端提供 HTTP 响应

我不明白为什么会发生这种情况,它只是在使用新的 golang docker 引擎后才发生(旧的“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

来自debian

MAINTAINER Leadis 之旅

LABEL Description="此docker镜像用于编译和执行用户程序。"

标签版本="0.1"

音量 /root/host/

运行 apt-get 更新 && 是 | apt-get 升级

运行是 | apt-get install gcc g++ python3 make

复制 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,但我仍然得到同样的错误:

连接期间出错:发布 https://%2Fvar%2Frun%2Fdocker.sock/v1.23/containers/create:http: 服务器向 HTTPS 客户端提供 HTTP 响应

【问题讨论】:

  • 看起来您的项目启用了 https,但您的本地 docker 守护进程仍然是纯 http。也许这个文档可以帮助docs.docker.com/engine/security/https
  • 您传递给 docker 实例的命令行选项是什么? (ps -ef | grep docker 你的/etc/docker/daemon.json 里有什么?你得到curl --unix-socket /var/run/docker.sock http:/v1.23/containers/json?all=1 的标准回复

标签: sockets go docker


【解决方案1】:
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/docker.sock
  2. http://localhost:2375
  3. https://localhost:2376

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-28
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 2021-05-13
    • 2018-03-06
    • 1970-01-01
    • 2020-11-09
    相关资源
    最近更新 更多