douzhanyan5015
2017-09-25 18:52
浏览 157

使用gRPC Docker容器构建Go时出错

I am trying to turn my golang grpc application into a docker container, however i keep getting an error when trying to build.

The error is

 messagepb/stickynote.pb.go:16:8: cannot find package "github.com/golang/protobuf/proto" in any of:
        /usr/local/go/src/github.com/golang/protobuf/proto (from $GOROOT)
        /go/src/github.com/golang/protobuf/proto (from $GOPATH)
sticky.go:6:2: cannot find package "github.com/llgcode/draw2d" in any of:
        /usr/local/go/src/github.com/llgcode/draw2d (from $GOROOT)
        /go/src/github.com/llgcode/draw2d (from $GOPATH)
sticky.go:7:2: cannot find package "github.com/llgcode/draw2d/draw2dimg" in any of:
        /usr/local/go/src/github.com/llgcode/draw2d/draw2dimg (from $GOROOT)
        /go/src/github.com/llgcode/draw2d/draw2dimg (from $GOPATH)
messagepb/stickynote.pb.go:21:2: cannot find package "golang.org/x/net/context" in any of:
        /usr/local/go/src/golang.org/x/net/context (from $GOROOT)
        /go/src/golang.org/x/net/context (from $GOPATH)
messagepb/stickynote.pb.go:22:2: cannot find package "google.golang.org/grpc" in any of:
        /usr/local/go/src/google.golang.org/grpc (from $GOROOT)
        /go/src/google.golang.org/grpc (from $GOPATH)
main.go:7:2: cannot find package "google.golang.org/grpc/credentials" in any of:
        /usr/local/go/src/google.golang.org/grpc/credentials (from $GOROOT)
        /go/src/google.golang.org/grpc/credentials (from $GOPATH)

The Dockerfile is

FROM golang:latest

RUN mkdir -p /go/src/github.com/alhennessey92/mygoapp
ADD . /go/src/github.com/alhennessey92/mygoapp

WORKDIR /go/src/github.com/alhennessey92/mygoapp
EXPOSE 8080

RUN go install github.com/alhennessey92/mygoapp
ENTRYPOINT ["/go/bin/mygoapp"]

The file structure is as follows

go
-- src
---- github.com
------ alhennessey92
-------- mygoapp
---------- Dockerfile
---------- main.go
---------- sticky.go
---------- messagepb
------------ stickynote.pb.go
------------ stickynote.proto

This is the code for main.go

package main

import (
    "io"
    "net"
    "log"
    "google.golang.org/grpc/credentials"
    "golang.org/x/net/context"
    pb "github.com/alhennessey92/mygoapp/messagepb"
    "google.golang.org/grpc"
)


const (
    useSSL = false
)

type StickyNoteServer struct{}

var stickyNoteServer StickyNoteServer



func (s *StickyNoteServer) Get(ctx context.Context, r *pb.StickyNoteRequest) (*pb.StickyNoteResponse, error) {
    var sticky Sticky
    sticky.Message = r.Message
    sticky.Centered = false

    resp := &pb.StickyNoteResponse{}
    stickyBytes, err := sticky.DrawPNG(512, 512)
    resp.Image = *stickyBytes

    return resp, err
}
func (s *StickyNoteServer) Update(stream pb.StickyNote_UpdateServer) error {
    for {
        in, err := stream.Recv()
        if err == io.EOF {
            return nil
        }
        if err != nil {
            return err
        }

        var sticky Sticky
        sticky.Message = in.Message
        sticky.Centered = false

        resp := &pb.StickyNoteResponse{}
        stickyBytes, err := sticky.DrawPNG(512, 512)
        resp.Image = *stickyBytes

        if err := stream.Send(resp); err != nil {
            return err
        }
    }
    return nil
}

 //[START main]
func main() {


    var err error
    var lis net.Listener
    var grpcServer *grpc.Server
    if !useSSL {
        lis, err = net.Listen("tcp", ":8080")
        if err != nil {
            log.Fatalf("failed to listen: %v", err)
        }
        grpcServer = grpc.NewServer()
    } else {
        certFile := "ssl.crt"
        keyFile := "ssl.key"
        creds, err := credentials.NewServerTLSFromFile(certFile, keyFile)
        lis, err = net.Listen("tcp", ":443")
        if err != nil {
            log.Fatalf("failed to listen: %v", err)
        }
        grpcServer = grpc.NewServer(grpc.Creds(creds))
    }
    pb.RegisterStickyNoteServer(grpcServer, &stickyNoteServer)
    grpcServer.Serve(lis)
}

This is the code i am running in the terminal to build the docker container

docker build -t alhennessey92/mygoapptest .

The go code i am using is from the google cloud grpc demo stickynote - https://cloud.google.com/solutions/mobile/mobile-compute-engine-grpc

I think the error has something to do with using the command go get, however i am not sure

Thanks for your help

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanang58939 2017-09-26 03:05
    已采纳

    You either need to add go get to your docker file, or you need to vendor the third party libraries with a tool like dep.

    vendoring is better, but if you want to just download files in docker container, do:

    FROM golang:latest
    
    RUN mkdir -p /go/src/github.com/alhennessey92/mygoapp
    ADD . /go/src/github.com/alhennessey92/mygoapp
    
    WORKDIR /go/src/github.com/alhennessey92/mygoapp
    EXPOSE 8080
    
    RUN go get ./...
    RUN go install 
    ENTRYPOINT ["/go/bin/mygoapp"]
    
    已采纳该答案
    打赏 评论
  • douni9620 2019-03-18 13:40

    i faced similar problem as yours

    turns out i was not creating folder correctly i was missing these lines from docker file

     ADD . /go/src/github.com/grpc/app_folder_name
     WORKDIR /go/src/github.com/grpc/app_folder_name`
    

    this is my dockerfile

    FROM golang
    # Copy our file in the host contianer to our contianer
    ADD . /go/src/github.com/grpc/app_folder_name
    WORKDIR /go/src/github.com/grpc/app_folder_name
    RUN go get -u github.com/golang/protobuf/protoc-gen-go
    RUN go get -u google.golang.org/grpc
    RUN go get -u go.mongodb.org/mongo-driver/mongo
    RUN go get -u github.com/go-redis/redis
    RUN go install -v ./...
    # Generate binary file from our /app
    RUN go build /go/src/github.com/grpc/app_folder_name/app_server/app.go
    # Expose the ports used in server
    EXPOSE 50051:50051
    EXPOSE 27018:27018
    # Run the app binarry file 
    CMD ["./app"]
    

    `

    打赏 评论

相关推荐 更多相似问题