我尝试为我的Golang应用程序创建映像,但是Docker只写:
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)
我的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
项目结构:
如何解决这个问题?
答案 0 :(得分:0)
我找到了解决方案。一切都出错了,因为我在项目的根目录中使用了以下命令:
$ docker build -t accounting_server ./accounting
multiService/accounting
是我的建筑目录,因此图像中的文件层次结构如下所示:
/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 刚刚将本地/multiService/accounting
的内容复制到了映像的/multiService
目录中。所以我不得不稍微更改 Dockerfile :
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
然后用下一条命令构建容器:
$ docker build -t accounting_server -f ./accounting/Dockerfile .
仍然位于根目录中,因此所有内容均已正确复制到映像。
感谢所有回答!