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 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?