2019-08-25 17:38
浏览 224


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.

  |--- Dockerfile 
  |--- main.go 
  |--- routes/ 
  |--- public/ 

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

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

My golang main.go

package main 

import (
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 
 | ---路由/  
 | --- public / 

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


  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 
“ github  .com / gorilla / mux“ 
” github.com/user/server/routes"
“ log” 
“ net / http” 
“ time” 
func main(){
 //  ... 



  main.go:5:2:找不到软件包“ github.com/ 用户/服务器/路由”中的任何一个:
 /usr/local/go/src/github.com/user/server/routes(来自$ GOROOT)
 /go/src/github.com/user/server/ 路线(来自$ 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

    打赏 评论

相关推荐 更多相似问题