gpt4 book ai didi

sockets - 使用 golang docker 引擎创建容器时出错

转载 作者:IT王子 更新时间:2023-10-29 02:22:44 25 4
gpt4 key购买 nike

我在 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: server gave HTTP response to HTTPS client

我不明白为什么会发生这种情况,它只是在使用新的 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

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

最佳答案

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

关于sockets - 使用 golang docker 引擎创建容器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39840235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com