【发布时间】: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的标准回复