因此,我尝试使用包含主文件补充代码的不同目录对 golang 应用程序进行 dockerize。我正在使用gorilla/mux。目录结构如下所示。


$GOPATH/src/github.com/user/server

  |--- Dockerfile 

  |--- main.go 

  |--- routes/ 

         handlers.go 

  |--- public/ 

         index.gohtml 

它在我的主机上运行没有问题。问题是,当我尝试部署 docker 映像时,它不会运行并在创建后不久退出。我尝试将dockerfile 中的WORKDIR命令更改为/go/src并将所有文件转储到那里,但仍然没有成功。我还尝试了docker hub上的官方文档。也不行。


我的 Dockerfile。


FROM golang:latest  

WORKDIR /go/src/github.com/user/server

COPY . .

RUN go get -d github.com/gorilla/mux


EXPOSE 8000

CMD ["go","run","main.go"]

我的 golang main.go


package main 


import (

    "github.com/gorilla/mux"

    "github.com/user/server/routes"

    "log"

    "net/http"

    "time"

)

func main(){

  //... 

}

当我检查 docker 映像的日志时,我收到此错误消息。


错误信息


main.go:5:2: cannot find package "github.com/user/server/routes" in any of:

    /usr/local/go/src/github.com/user/server/routes (from $GOROOT)

    /go/src/github.com/user/server/routes (from $GOPATH)