0

So this my docker file for building docker image for Go which uses dep package manager.

FROM golang:1.8.5-jessie

# install dep
RUN go get github.com/golang/dep/cmd/dep

# create a working directory
WORKDIR /go/src/app

# add Gopkg.toml and Gopkg.lock
ADD Gopkg.toml Gopkg.toml
ADD Gopkg.lock Gopkg.lock

# install packages
RUN dep ensure --vendor-only

# add source code
ADD src src

# run main.go
CMD ["go", "run", "src/main.go"]

Gopkg.toml looks like this:

[[constraint]]
  name = "github.com/spf13/viper"
  version = "1.1.0"

[[constraint]]
  name = "github.com/gin-gonic/gin"
  version = "1.3.0"

[prune]
  go-tests = true
  unused-packages = true

Docker image builds, but then I try to run it I get these errors:

src/requestHandler/requestHandler.go:4:2: cannot find package "_/go/src/app/vendor/github.com/gin-gonic/gin" in any of:
        /usr/local/go/src/_/go/src/app/vendor/github.com/gin-gonic/gin (from $GOROOT)
        /go/src/_/go/src/app/vendor/github.com/gin-gonic/gin (from $GOPATH)
src/configReader/configReader.go:4:2: cannot find package "_/go/src/app/vendor/github.com/spf13/viper" in any of:
        /usr/local/go/src/_/go/src/app/vendor/github.com/spf13/viper (from $GOROOT)
        /go/src/_/go/src/app/vendor/github.com/spf13/viper (from $GOPATH)

It looks like import from vendor package is missing. Can anyone could give me a hint how to fix it?

Project structure as follows:

myproject
  |-src
    |-httpRouter
    |  |-httpRouter.go
    |-requesthandler
       |-requesthandler.go
    |-main.go
  |-vendor
  |-Gopkg.toml
  |-Gopkg.lock

httpRouter source code:

package httpRouter

import (
    "github.com/gin-gonic/gin"
    "../requestHandler"
)

func SetupRouter() *gin.Engine {
    router := gin.Default()
    v1 := router.Group("api/v1")
    {
        v1.POST("/submit", requestHandler.HandleSubmitEmailRequest)
    }
    return router
}

UPDATE changed relative paths:

Getting these errors now.

vendor/github.com/gin-gonic/gin/binding/protobuf.go:11:2: cannot find package "github.com/golang/protobuf/proto" in any of:
            /go/src/notification-gateway/vendor/github.com/golang/protobuf/proto (vendor tree)
            /usr/local/go/src/github.com/golang/protobuf/proto (from $GOROOT)
            /go/src/github.com/golang/protobuf/proto (from $GOPATH)
    vendor/github.com/gin-gonic/gin/logger.go:14:2: cannot find package "github.com/mattn/go-isatty" in any of:
            /go/src/notification-gateway/vendor/github.com/mattn/go-isatty (vendor tree)
            /usr/local/go/src/github.com/mattn/go-isatty (from $GOROOT)
            /go/src/github.com/mattn/go-isatty (from $GOPATH)
    vendor/github.com/gin-gonic/gin/binding/msgpack.go:12:2: cannot find package "github.com/ugorji/go/codec" in any of:
            /go/src/notification-gateway/vendor/github.com/ugorji/go/codec (vendor tree)
            /usr/local/go/src/github.com/ugorji/go/codec (from $GOROOT)
            /go/src/github.com/ugorji/go/codec (from $GOPATH)
    vendor/github.com/gin-gonic/gin/binding/default_validator.go:11:2: cannot find package "gopkg.in/go-playground/validator.v8" in any of:
            /go/src/notification-gateway/vendor/gopkg.in/go-playground/validator.v8 (vendor tree)
            /usr/local/go/src/gopkg.in/go-playground/validator.v8 (from $GOROOT)
            /go/src/gopkg.in/go-playground/validator.v8 (from $GOPATH)
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • can you post file structure of your project? – xReprisal Aug 19 '18 at 08:54
  • Show your go source code that declares the imports, because those imports look wrong. Are you actually importing the packages using the full path? `"_/go/src/app/vendor/github.com/spf13/viper"`, if so you shouldn't do that, your imports should look like this `"github.com/spf13/viper"` – mkopriva Aug 19 '18 at 09:14
  • I have added project structure to post @xReprisal – Romas Augustinavičius Aug 19 '18 at 09:19
  • I have updated post with source code of httpRouter.go source code. @mkopriva – Romas Augustinavičius Aug 19 '18 at 09:20
  • @RomasAugustinavičius although I'm not sure if this is the cause of your problem do not do relative imports in Go. – mkopriva Aug 19 '18 at 09:24
  • @RomasAugustinavičius first fix the relative import (https://stackoverflow.com/a/38518202/965900) and if after that you still get the same error let me know. – mkopriva Aug 19 '18 at 09:26
  • @mkopriva I did that, I have updated post with outcome. It seems that now it finds gin framework. – Romas Augustinavičius Aug 19 '18 at 09:45
  • @RomasAugustinavičius The problem seems to be your incomplete `Gopkg.toml` file and the use of the `--vendor-only` flag. If I understand it correctly that flag tells `dep` to install only what is in the `Gopkg` file and not worry about any other imports, so there is no resolving being done for other packages that you may have imported or that were imported by for example `gin`. – mkopriva Aug 19 '18 at 09:52
  • @RomasAugustinavičius try not using the `--vendor-only` flag, and if that doesn't work then you'll have to add the missing packages to the manifest file. I'm not a `dep` user so I don't know for sure but I assume it provides some way of adding the imports to the manifest file automatically, so that you don't have to do it manually. – mkopriva Aug 19 '18 at 09:55

0 Answers0