代码之家  ›  专栏  ›  技术社区  ›  jjuser19jj

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

  •  0
  • jjuser19jj  · 技术社区  · 7 年前

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

    但对于每个私有依赖项,我都会得到以下错误:

    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)
    

    RUN go get ./.. RUN go build -o main . ,我得到每个包的以下错误:

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

    我尝试过几种解决方案,但都不奏效。我总是会犯上述错误。由于这是我的第一个docker+golang项目,是否有任何可用于golang应用程序的docker文件,该应用程序具有公共和私有依赖关系?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Dmitry Harnitski    7 年前

    GOPATH/src/<import-path> 你可以用你的方式来管理它们 go get 工具。

    考虑 vendoring dep 或 modules

    您还可以改进您的创建方式 Docker 形象。

    GO 工具链。代码被复制到该容器中,容器编译并承载代码。生产只需要晚一点。

    更好的选择是使用2个容器:

    1. Go编译工具
    2. 仅承载二进制文件的轻量级容器
    # 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"]
    
    推荐文章