douzhanyan5015 2017-09-25 18:52
浏览 163
已采纳

使用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"]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 个人通讯录管理系统 C语言 程序设计 需要能运行成功的
  • ¥15 数电设计题 没有设计思路 不知道用什么芯片进行设计 求提供设计思路
  • ¥15 在动态多目标优化问题中,第一幅图展示的是问题DF6的相关定义和绘制的POS和POF图,请问图中公式PS(t)和PF(t)是如何推导的
  • ¥60 设计一种优化算法结合案例给出智能仓储四向穿梭车的调度计划
  • ¥15 Errno2:No such file or directory,在当前文件确实没有该图片,怎么解决?
  • ¥15 博世摄像头数据存储的问题(iscsi)
  • ¥15 如何实现对学生籍贯信息管理系统的选择排序
  • ¥15 写一个51单片机的时钟代码
  • ¥15 git clone报错
  • ¥15 3d-slicer超声造影动态图像导入报错