与docker的godep供应商

我试图与供应商运行一个docker集装箱。 这是我的Dockerfile

FROM golang:alpine EXPOSE 8080 RUN mkdir /app ADD . /app/ WORKDIR /app RUN go build -o myapp . CMD ["/app/myapp"] 

和我的main.go

 package main import ( "fmt" "log" "net/http" "github.com/gorilla/mux" ) func main() { r := mux.NewRouter() r.HandleFunc("/", Hello) http.Handle("/", r) fmt.Println("Starting up on 8080") log.Fatal(http.ListenAndServe(":8080", nil)) } func Hello(w http.ResponseWriter, req *http.Request) { fmt.Fprintln(w, "Hello world!") } 

我正在使用godep进行自定义库,它在我的本地机器上工作,但是当我试图用docker运行它时:

 docker build -t myapp-img . docker run -p 8080:8080 --name myapp-cnt myapp-img 

我有以下错误:

 main.go:8:2: cannot find package "github.com/gorilla/mux" in any of: /usr/local/go/src/github.com/gorilla/mux (from $GOROOT) /go/src/github.com/gorilla/mux (from $GOPATH) 

我不明白什么是缺less的。

错误是正确的。 它告诉你一切有抱负的Gopher需要的东西。

我假设你已经将Gorilla Mux复制到本地机器的应用程序/供应商目录中,如下所示:

 ./main.go # this is your myapp code you are coping ./vendor/github.com/gorilla/mux # for vendoring, this must exist 

如果你想了解更多关于开拓,请看这里我的受欢迎的答案:

我应该如何在Go 1.6中使用供应商?

现在,要解决这个错误假设你已经完成了上述…

Gopher需要设置一个有效的$GOPATH才能构build。 这是从您的Dockerfile丢失。

 FROM golang:1.7-alpine EXPOSE 8080 # setup GOPATH and friends # # TECHNICALLY, you don't have to do these three cmds as the # golang:alpine image actually uses this same directory structure and # already has $GOPATH set to this same structure. You could just # remove these two lines and everything below should continue to work. # # But, I like to do it anyways to ensure my proper build # path in case I experiment with different Docker build images or in # case the #latest image changes structure (you should really use # a tag to lock down what version of Go you are using - note that I # locked you to the docker image golang:1.7-alpine above, since that is # the current latest you were using, with bug fixes). # RUN mkdir -p /go/src \ && mkdir -p /go/bin \ && mkdir -p /go/pkg ENV GOPATH=/go ENV PATH=$GOPATH/bin:$PATH # now copy your app to the proper build path RUN mkdir -p $GOPATH/src/app ADD . $GOPATH/src/app # should be able to build now WORKDIR $GOPATH/src/app RUN go build -o myapp . CMD ["/go/src/app/myapp"] 

这里工作…

 $ tree . ├── Dockerfile ├── main.go └── vendor └── mydep └── runme.go 

我的应用程序的源文件:

 $ cat main.go package main import ( "fmt" "mydep" ) func main() { fmt.Println(mydep.RunMe()) } 

我的vendor/文件夹依赖:

 $ cat vendor/mydep/runme.go package mydep // RunMe returns a string that it worked! func RunMe() string { return "Dependency Worked!" } 

现在,构build并运行图像:

 $ docker build --rm -t test . && docker run --rm -it test (snip) Step 8 : WORKDIR $GOPATH/src/app ---> Using cache ---> 954ed8e87ae0 Step 9 : RUN go build -o myapp . ---> Using cache ---> b4b613f0a939 Step 10 : CMD /go/src/app/myapp ---> Using cache ---> 3524025080df Successfully built 3524025080df Dependency Worked! 

请注意最后一行输出来自控制台的输出, Dependency Worked!

这是因为:

  1. 你说你正在使用Vendor,这意味着你的应用程序代码的根目录中有一个名为./vendor的本地目录。
  2. 当你ADD . /go/src/app ADD . /go/src/app ,您也正在将./vendor本地复制到您的应用程序代码。
  3. 您已将文件复制到Go构build工具所需的适当的$GOPATH安装结构中,以查找包(在本例中为源代码的根文件夹中的./vendor目录)。