Docker alpine下使用go get下载coding.net私有仓库代码出错

6

我使用coding.net创建了私有代码仓库。
我使用的docker镜像为alpinecentos
我可以从docker-centos顺利获取git.coding.net/alphayan/orionv2.git,但是无法从docker-alpine中获取git.coding.net/alphayan/test.git。它返回一个错误信息:

/go/src # go get -u -v  git.coding.net/alphayan/test.git
# cd .; git ls-remote https://git.coding.net/alphayan/test
fatal: could not read Username for 'https://git.coding.net': terminal prompts disabled
# cd .; git ls-remote git+ssh://git.coding.net/alphayan/test
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
# cd .; git ls-remote ssh://git.coding.net/alphayan/test
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
package git.coding.net/alphayan/test.git: cannot download, git.coding.net/alphayan/test uses insecure protocol

从CentOS中,它让我使用用户名和密码:

[root@83fc8067fc95 /]# go get -u -v git.coding.net/alphayan/test.git
Username for 'https://git.coding.net':

最后,我发现这是由git的版本引起的问题。CentOS使用的是git 1.8.3,而alpine使用的是git 2.11.0。
然后我将CentOS的git版本更改为2.11.0,结果与alpine一样出错了。 我认为我可以修改golang或git源文件来解决这个问题,有人能帮忙吗?谢谢~!


什么错误?另外,看起来你有一个私有的Git仓库。 - Yandry Pozo
使用Alpine,git的版本是2.11。它不让我输入用户名和密码,因此我无法获取代码;但是使用CentOS,git的版本是1.8.3。它提示我输入用户名和密码,然后我可以获取代码。我将git版本从2.11更改为CentOS,与Alpine一样,出现相同的问题。我发现这是git的错误。 - alphayan
2个回答

4
这个错误的原因是默认情况下go get不使用终端输入。可以通过修改环境变量GIT_TERMINAL_PROMPT来改变这种行为,这个变量在git 2.3中被引入。这就是为什么在CentOS 7(git 1.8)和Alpine 3.5(git 2.11)中,go get命令表现不同的原因。
git >= 2.3中,你可以通过以下方式解决这个问题,运行go get
$ GIT_TERMINAL_PROMPT=1 go get github.com/foo/bar
Username for 'https://github.com':

如果您有多个go get命令,则可以在运行命令之前导出该环境变量:
$ export GIT_TERMINAL_PROMPT=1
$ go get github.com/foo/bar
Username for 'https://github.com':
$ go get github.com/foo/baz
Username for 'https://github.com':

那似乎比我的答案更精确。+1 - VonC
非常感谢。 - alphayan

3

如果您在coding.net上注册了公共SSH密钥,则可以尝试通过SSH进行操作。

以"在Docker中获取私有存储库的示例"为例:

FROM golang:1.6

RUN echo "[url \"git@github.com:\"]\n\tinsteadOf = https://github.com/" >> /root/.gitconfig
RUN mkdir /root/.ssh && echo "StrictHostKeyChecking no " > /root/.ssh/config
ADD .  /go/src/github.com/company/foo
CMD cd /go/src/github.com/company/foo && go get github.com/company/bar && go build -o /foo

使用构建步骤:
docker build -t foo-build .
docker run --name=foo-build -v ~/.ssh/id_rsa:/root/.ssh/id_rsa foo-build
docker cp foo-build:/foo foo
docker rm -f foo-build
docker rmi -f foo-build 

是的,通过ssh应该没问题。我发现在git版本1.8.4之后,go get无法从私有仓库成功获取代码,但使用git clone可以获取到代码。我不知道这是git还是golang的bug。 - alphayan
@alphayan 我也不知道,但如果这能让我们继续前进,这个答案可能是一个解决方案。 - VonC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接