du5739 2017-12-15 17:26
浏览 99
已采纳

使用Go应用构建Docker:找不到软件包

I have my Dockerfile in the root of directory with src/myapp folder, myapp contains myapp.go with main package.

Dockerfile looks like following:

FROM golang:1.9.2

ADD . /
RUN go build myapp;

ENTRYPOINT ["/go/bin/myapp"]

I get following error:

can't load package: package myapp: cannot find package "myapp" in any of:
    /usr/local/go/src/myapp (from $GOROOT)
    /go/src/myapp (from $GOPATH)

What am I doing wrong? Can I log ls command after docker has done ADD?

  • 写回答

6条回答 默认 最新

  • dragonlew9876 2017-12-15 17:39
    关注

    You are copying all the files to Image root directory, Didn't installed any dependencies, Trying to Build it and then run the binary from /go/bin/app. The binary doesn't exists in that directory and it's generating errors.

    I would recommend using a Dockerfile like this,

    FROM golang:1.9.2 
    ADD . /go/src/myapp
    WORKDIR /go/src/myapp
    RUN go get myapp
    RUN go install
    ENTRYPOINT ["/go/bin/myapp"]
    

    This'll do the following.

    1. Copy project files to /go/src/myapp.
    2. Set Working directory to /go/src/myapp.
    3. Install dependencies, I used go get but replace it with which ever dependency management tool you are using.
    4. Install/build the binary.
    5. Set entry point.

    You can run ls or any other command using docker exec.

    Example:

    docker exec <image name/hash> ls
    

    You can also enter the shell in the generated image to understand it well using

    docker run --rm -it <image hash/name> /bin/sh
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么