doushui3061
2019-03-25 17:49
浏览 120

无法使用Docker为Golang应用创建映像

I try to create an image for my Golang application, but Docker just writes:

Step 7/9 : RUN go install ./accounting/server
 ---> Running in f998ba6a5efb
can't load package: package grpcProjects/multiService/accounting/server: cannot find package "grpcProjects/multiService/accounting/server" in any of:
        /usr/local/go/src/grpcProjects/multiService/accounting/server (from $GOROOT)
        /go/src/grpcProjects/multiService/accounting/server (from $GOPATH)

My Dockerfile:

FROM golang:1.10.4

ADD . /go/src/grpcProjects/multiService

WORKDIR /go/src/grpcProjects/multiService

RUN go get github.com/golang/protobuf/proto
RUN go get golang.org/x/net/context
RUN go get google.golang.org/grpc
RUN go install ./accounting/server

ENTRYPOINT [ "/go/bin/server" ]

EXPOSE 8080

Project structure:

enter image description here

How to solve this problem?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • douqi2804 2019-03-30 16:05
    最佳回答

    I've found a solution. Everything went wrong because I used this command from the root directory of the project:

    $ docker build -t accounting_server ./accounting
    

    And multiService/accounting was my building directory, so file hierarchy in the image looked like that:

    /go/src/grpcProjects/multiService
    /go/src/grpcProjects/multiService/server
    /go/src/grpcProjects/multiService/server/service
    /go/src/grpcProjects/multiService/server/service/accounting.go
    /go/src/grpcProjects/multiService/server/main.go
    /go/src/grpcProjects/multiService/proto
    /go/src/grpcProjects/multiService/proto/accounting.proto
    /go/src/grpcProjects/multiService/proto/accounting.pb.go
    /go/src/grpcProjects/multiService/Dockerfile
    

    Docker just copied the contents of local /multiService/accounting to /multiService directory of the image. So I had to change Dockerfile a bit:

    FROM golang:1.10.4
    
    ADD . /go/src/grpcProjects/multiService
    
    RUN go get github.com/golang/protobuf/proto
    RUN go get golang.org/x/net/context
    RUN go get google.golang.org/grpc
    RUN go install grpcProjects/multiService/accounting/server
    
    ENTRYPOINT [ "/go/bin/server" ]
    
    EXPOSE 8080
    

    And I built my container with the next command:

    $ docker build -t accounting_server -f ./accounting/Dockerfile .
    

    Still from the root directory so everything was copied to the image properly.

    Thanks to everyone who replied!

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题