duanne9313
2019-08-25 17:38
浏览 267

Golang在Docker映像中找不到软件包

So, I am trying to dockerize a golang application with different directories containing supplementary code for my main file. I am using gorilla/mux. The directory structure looks like this.

$GOPATH/src/github.com/user/server
  |--- Dockerfile 
  |--- main.go 
  |--- routes/ 
         handlers.go 
  |--- public/ 
         index.gohtml 

It works on my host machine with no problem. The problem is that when I try to deploy the docker image it does not run and exits shortly after creation. I have tried changing the WORKDIR command in my dockerfile to /go/src and dump all my files there, but still no luck. I have also tried the official documentation on docker hub. Doesn't work either.

My 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"]

My golang main.go

package main 

import (
    "github.com/gorilla/mux"
    "github.com/user/server/routes"
    "log"
    "net/http"
    "time"
)
func main(){
  //... 
}

I get this error message when I check the logs of my docker image.

Error Message

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)

图片转代码服务由CSDN问答提供 功能建议

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

  $ GOPATH / src / github.com / user / server 
 | --- Dockerfile 
 | --- main.go 
 | ---路由/  
 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:找不到软件包“ github.com/ 用户/服务器/路由”中的任何一个:
 /usr/local/go/src/github.com/user/server/routes(来自$ GOROOT)
 /go/src/github.com/user/server/ 路线(来自$ GOPATH)
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题