我正在尝试使用docker的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
并在导入而不是"github.com/my_fabric/go-connections/nat"
中使用"github.com/docker/go-connections/nat"
。但是我得到了以下错误。
vignesh@vignesh-ThinkPad-E470 ~/go-book/src/github.com/my_fabric $ go build asd.go
# command-line-arguments
./asd.go:25:29: cannot use "github.com/my_fabric/vendor/github.com/my_fabric/go-connections/nat".PortSet literal (type "github.com/my_fabric/vendor/github.com/my_fabric/go-connections/nat".PortSet) as type "github.com/docker/docker/vendor/github.com/docker/go-connections/nat".PortSet in field value
基本上我想使用docker的存储库中的vendor目录中的包。请帮助:)
它只会尝试在docker环境中为vendor目录提供用户权限。 Beucase导入包的供应商路径是正确的。
在golang中导入的工作原理如下: - 首先,它将从供应商目录导入包,否则它将查找相同包的
$GOPATH
src目录。
该错误表示无法在任何给定路径中找到包。但是你在供应商目录中有它,所以这可能是任何权限问题。
因为如果您正在使用Linux,则权限将不允许访问供应商目录。
最好不要在docker中使用Gopkg.toml
复制而不是生成供应商包。
这两个目录不等价:
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
您根本不必更改import语句。