无法使用Docker存储库中供应商目录中的软件包

时间:2019-02-20 05:07:49

标签: docker go docker-api

我正在尝试使用docker's go api创建一个容器。我想使用container.Config.ExposedPorts API中的ContainerCreate()公开端口。下面是代码

package main

import (
        "fmt"
        "context"
        "github.com/docker/docker/api/types/container"
        "github.com/docker/docker/client"
        "github.com/docker/go-connections/nat"
)

func main() {

        ctx := context.Background()
        cli, err := client.NewClientWithOpts(client.WithVersion("1.38"))
        if err != nil {
                fmt.Println("Failed to get container envoronment", err)
        }   

        resp, err := cli.ContainerCreate(ctx, &container.Config{
                Image: "hyperledger/fabric-ca",
                Cmd:   []string{"/bin/sh", "-c", "fabric-ca-server start -b admin:adminpw"},
                Env: []string{"FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server",
                        "FABRIC_CA_SERVER_CA_NAME=ca.example.com"},
                ExposedPorts: nat.PortSet{"22/tcp":struct{}{},},
        }, nil, nil, "ca.example.com")


        if err != nil {
                fmt.Println(" failed to create container, err:", err)
        } else {
                fmt.Println(" Container ID :", resp.ID, "warning:", resp.Warnings, "err:", err)
        }   
}

编译时出现以下错误

vignesh@vignesh-ThinkPad-E470 ~/go-book/src/github.com/my_fabric $ go build asd.go 
asd.go:8:9: cannot find package "github.com/docker/go-connections/nat" in any of:
    /home/vignesh/go-book/src/github.com/my_fabric/vendor/github.com/docker/go-connections/nat (vendor tree)
    /usr/local/go/src/github.com/docker/go-connections/nat (from $GOROOT)
    /home/vignesh/go-book/src/github.com/docker/go-connections/nat (from $GOPATH)

由于软件包"github.com/docker/go-connections/nat"位于"github.com/docker/docker/vendor/github.com/docker/go-connections/nat"的供应商目录中,因此我在工作目录中创建了一个供应商目录,并将github.com/docker/docker/vendor/github.com/docker/go-connections/nat的内容复制到github.com/my_fabric/vendor/go-connections/nat并使用了{导入时为{1}},而不是"github.com/my_fabric/go-connections/nat"。但是我遇到了以下错误。

"github.com/docker/go-connections/nat"

基本上我想使用docker存储库中供应商目录中的软件包。请帮助:)

2 个答案:

答案 0 :(得分:0)

仅在docker环境中尝试向用户提供供应商目录的权限时,它会起作用。用Beucase导入软件包的供应商路径正确。

  

导入golang的方式如下:-首先,它将从中导入包   如果没有供应商目录,它将寻找$GOPATH的src目录   相同的包裹。

该错误表明它无法在任何给定路径下找到该软件包。但是您将其保存在vendor目录中,因此可能是任何权限问题。

由于发生这种情况(如果您使用的是Linux),该权限将不允许访问供应商目录。

另外,最好不要复制而不是在docker中使用Gopkg.toml生成供应商软件包。

答案 1 :(得分:0)

这两个目录不相同:

github.com/docker/docker/vendor/github.com/docker/go-connections/nat
github.com/my_fabric/vendor/go-connections/nat

您必须将所有Docker的供应商依赖项保持不变(而不是复制)到您自己的供应商目录中,例如以下目录应该存在:

github.com/my_fabric/vendor/github.com/docker/go-connections/nat

请注意github.com/docker段。如果您复制目录,您将得到两个软件包的副本,这将给您带来麻烦。例如,您最终得到了不同的类型

"github.com/docker/docker/vendor/github.com/docker/go-connections/nat".Port
"github.com/docker/go-connections/nat".Port

您根本不需要更改导入语句。

相关问题