docker - 无法使用 docker 存储库中 vendor 目录中的包

标签 docker go docker-api

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

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 的 vendor 目录中/go-connections/nat",然后我在我的工作目录中创建了一个 vendor 目录并复制了 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 目录的用户权限,它就会起作用。因为导入包的 vendor 路径是正确的。

Import in golang works as:- First it will import the package from vendor directory if not it will look for $GOPATH src directory for the same package.

错误表明它无法在任何给定路径中找到包。但是你在 vendor 目录中有它,所以这可能是任何权限问题。

因为如果您在 Linux 上工作,权限将不允许访问 vendor 目录。

此外,最好不要复制,而是在 docker 中使用 Gopkg.toml 生成 vendor 包。

关于docker - 无法使用 docker 存储库中 vendor 目录中的包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54779211/

相关文章:

ubuntu - docker ps 命令在哪里查找当前容器?

regex - 获取Golang正则表达式中括号内的所有子字符串

parsing - 为什么在 Go 中使用 := with fmt. Scanf 总是返回 1?

go - 如何使用 gb 获得代码覆盖率?

windows - 如何从运行 Docker Toolbox(docker 机器)的 Windows 启用 Docker API 访问

docker - Docker 容器中的 SizeRootFs 和 SizeRw 之间的确切区别是什么?

docker - 部署 nginx 的最佳实践

laravel - 使用Laradock运行Elastic Search

docker - 使用Docker Compose运行多个GlassFish

docker - 通过 Docker API 将私有(private)镜像推送到 Docker Registry