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

我正在尝试在Docker容器中获取Go应用。 这是我的第一个更大的Go and Docker项目。 只要我在本地计算机上运行go程序,它就可以正常运行,现在我想在docker容器中的EC2上运行它。 我的docker文件如下所示:</ p>

  FROM golang:latest 
RUN mkdir / tir
ADD。 / tir
WORKDIR / tir
RUN转到build -o main。

CMD [“ / app / main”]
</ code> </ pre>

但是对于每个私有依赖项,我都会收到以下错误:</ p>

 < 代码> main.go:17:2:在以下任何位置都找不到软件包“ github.com/ser/model”:
/usr/local/go/src/github.com/ser/model(来自$ GOROOT)\ n /go/src/github.com/ser/model(来自$ GOPATH)
</ code> </ pre>

当我插入 RUN时,获取./..</ RUN运行build -o main之前。</ code>,对于每个软件包,我都会收到以下错误:</ p>

 致命:无法读取'https的用户名 ://github.com':终端提示已禁用
</ code> </ pre>

包github.com/ser/endpoints:退出状态128 </ p>

我尝试了几种解决方案,但没有一个奏效。 我总是最终遇到上述错误。 既然这是我的第一个docker + golang项目,那么有没有准备好将dockerfiles用于具有公共和私有依赖关系的golang应用程序?</ p>

UPDATE:
我卸载了go,并复制了每个 文件一个接一个地使用,并在每个文件之后使用dep-确保。 现在可以了,谢谢:D </ p>
</ div>

展开原文

原文

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

dscs63759
dscs63759 请参阅此链接stackoverflow.com/questions/55058056/…
一年多之前 回复
dousao2186
dousao2186 可能重复的“去获取”私有存储库的正确方法是什么?
接近 2 年之前 回复

1个回答

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"]
doumei1955
doumei1955 将此评论添加为更新:添加到您的问题中以帮助他人并将答案标记为正确
接近 2 年之前 回复
dongxian7471
dongxian7471 我卸载了go,并一一复制了每个文件,并在每个文件后使用了dep -ensure。 现在可以了,谢谢:D
接近 2 年之前 回复
dsp1836
dsp1836 通过供应商,您所有的依赖项都应存储在项目本地目录内的/ vendor文件夹中,并应提交到源代码管理中。
接近 2 年之前 回复
doupo2241
doupo2241 我只是通过项目中使用的路径und像其他任何软件包一样导入了它。 我不得不说我试图从我的项目中删除所有非公共软件包,但是我仍然遇到相同的错误。 FX也适用于大猩猩/多路复用器
接近 2 年之前 回复
dsfs64664
dsfs64664 github.com/aristotle/auth0不是公共存储库。 在添加dep之前,您是如何使用它的?
接近 2 年之前 回复
duanjia9577
duanjia9577 我尝试使用dep,但遇到以下问题1可传递有效的内部软件包从11个项目导入的14个外部软件包(0)–选择(根)(1)– github.com/aristotle/auth0没有更多版本 尝试 开始回溯–按段求解失败的规划求解时间:b-list-versions:39.248856824s其他:39.136849ms select-root:29.564742ms new-atom:954.931µs b-ducuce-proj-root:26.362µs b-source -存在:490ns共:39.318540198s个想法?
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐