dongweng9474 2018-11-16 16:15
浏览 516
已采纳

如何在Docker中安装私有依赖项

I am trying to get a Go app inside a docker container. This is my first bigger Go and Docker project. The go program runs just fine as long as I am running it on my local machine, now I want to run it on EC2 within a docker container. My docker file looks like this:

FROM golang:latest 
RUN mkdir /tir
ADD . /tir 
WORKDIR /tir
RUN go build -o main . 
CMD ["/app/main"]

But I get the following error for every private dependencies:

main.go:17:2: cannot find package "github.com/ser/model" in any of:
    /usr/local/go/src/github.com/ser/model (from $GOROOT)
    /go/src/github.com/ser/model (from $GOPATH)

When I insert RUN go get ./.. before RUN go build -o main ., I get the following error for every package:

fatal: could not read Username for 'https://github.com': terminal prompts disabled

package github.com/ser/endpoints: exit status 128

I have tried a couple of solutions, but none worked. I always end up having the above errors. Since this is my first docker + golang project, are there any ready to use dockerfiles for an golang app with public as well as private dependencies?

UPDATE: I deinstalled go, and copied every file in one by one and used dep -ensure after every file. Now it works, thanks :D

  • 写回答

1条回答

  • douping3891 2018-11-16 17:50
    关注

    Your dependencies are probably stored in GOPATH/src/<import-path> and you manage them with go get tool.

    Consider vendoring and tools like dep or modules

    As a result your dependencies will be included into source control and project will be much more portable.

    You also can improve the way how you create Docker image.

    Current implementation uses one container that includes whole GO toolchain. Code is copied inside that that container, container compiles and hosts the code. Only later is necessary for production.

    Better option will be to use 2 containers:

    1. Go Tools for compilation
    2. Lightweight container that only host binaries
    # Debian image with the latest version of Go installed
    # and a workspace (GOPATH) configured at /go.
    FROM golang:1.11 as builder
    WORKDIR /go/src/github.com/space/project/
    
    # Copy the local package files to the container's workspace.
    ADD . /go/src/github.com/space/project/
    
    # Build the service inside the container.
    RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
    
    FROM centurylink/ca-certs
    EXPOSE 8080
    
    # Copy app
    COPY --from=builder /go/src/github.com/space/project/app   /
    ENTRYPOINT ["/app"]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛