0

I am trying to create a container with docker's go api. I want to expose a port using container.Config.ExposedPorts in ContainerCreate()API. Below is the code

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)
        }   
}

when I compile I get the below error

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)

As package "github.com/docker/go-connections/nat" is in a vendor directory at "github.com/docker/docker/vendor/github.com/docker/go-connections/nat", I then created a vendor directory in my working directory and copied contents of github.com/docker/docker/vendor/github.com/docker/go-connections/nat to github.com/my_fabric/vendor/go-connections/nat and used "github.com/my_fabric/go-connections/nat" in import rather than "github.com/docker/go-connections/nat". But I got the following error.

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

Basically I want to use packages which is in vendor directory in docker's repository. Kindly help :)

Vignesh k
  • 131
  • 2
  • 10

2 Answers2

0

It will work just try to provide user permissions to the vendor directory in docker environment. Beucase the vendor path to import the package is correct.

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.

The error says it cannot find the package at any of the given paths. But you have it in the vendor directory so this can be any permission issue.

Since it happens if you are working on a linux the permissions will not allow to access the vendor directory.

Also it is better not to copy rather to generate a vendor package using Gopkg.toml in the docker.

Himanshu
  • 12,071
  • 7
  • 46
  • 61
  • follwoing is the permissions to vendor directory in docker enviroment. I think it is fine.Kindly let me kow if it is not fine `drwxrwxr-x 9 vignesh vignesh 4096 Feb 20 21:43 vendor` – Vignesh k Feb 20 '19 at 16:21
  • yes it is find but when you copy a new folder it will default to other permissions so try to run that recursively and try again. I solved the same issue using that. – Himanshu Feb 21 '19 at 09:45
0

Theses two directories are not equivalent:

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

You have to move (not copy) all of Docker's vendored dependencies unchanged into your own vendor directory, e.g. the following directory should exist:

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

Note the github.com/docker segments. If you copy the directories you'll end up with two copies of the packages, which will get you into trouble. For instance, you end up with the distinct types

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

You don't have to change your import statements at all.

Peter
  • 29,454
  • 5
  • 48
  • 60
  • I moved all of dockers vendored dependencies to my vendor directory. But then I am getting following errors ```../docker/docker/client/container_commit.go:9:2: cannot find package "github.com/docker/distribution/reference" in any of: /usr/local/go/src/github.com/docker/distribution/reference (from $GOROOT) /home/vignesh/go-book/src/github.com/docker/distribution/reference (from $GOPATH) ``` – Vignesh k Feb 20 '19 at 16:16