duanne9313 2019-08-25 17:38
浏览 277
已采纳

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)
  • 写回答

1条回答 默认 最新

  • dongyunshan4066 2019-08-25 17:45
    关注

    Try the following Docker file:

    # GO Repo base repo
    FROM golang:1.12.0-alpine3.9 as builder
    
    RUN apk add git
    
    # Add Maintainer Info
    LABEL maintainer="<>"
    
    RUN mkdir /app
    ADD . /app
    WORKDIR /app
    
    COPY go.mod go.sum ./
    
    # Download all the dependencies
    RUN go mod download
    
    COPY . .
    
    # Build the Go app
    RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
    
    # GO Repo base repo
    FROM alpine:latest
    
    RUN apk --no-cache add ca-certificates curl
    
    RUN mkdir /app
    
    WORKDIR /app/
    
    # Copy the Pre-built binary file from the previous stage
    COPY --from=builder /app/main .
    
    # Expose port 8000
    EXPOSE 8000
    
    # Run Executable
    CMD ["./main"]
    

    Here, we are creating an intermediate docker builder container, copying the code into it, build the code inside the builder container and then copy the binary image to the actual docker.

    This will help in both having all the dependencies in the final container and also, the size of the final image will be very small

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 将安全信息用到以下对象时发生以下错误:c:dumpstack.log.tmp 另一个程序正在使用此文件,因此无法访问
  • ¥15 速度位置规划实现精确定位的问题
  • ¥15 代码问题:df = pd.read_excel('c:\User\18343\Desktop\wpsdata.xlxs')路径读不到
  • ¥15 为什么视频算法现在全是动作识别?
  • ¥15 编写一段matlab代码
  • ¥15 用Python做岩石类别鉴定软件
  • ¥15 关于调取、提交更新数据库记录的问题
  • ¥15 之前删了盘从下vs2022遇见这个问题 搞了一整天了
  • ¥15 从Freecad中宏下载的DesignSPHysics,出现如下问题是什么原因导致的(语言-python)
  • ¥30 notepad++ 自定义代码补全提示