Docker:从私有GitHub仓库获取go代码。

27

我想运行一个容器,该容器将从我在私有GitHub repo上拥有的golang包中公开一个服务。

由于我正在使用GCE,我的起始镜像是google/debian:wheezy。

安装所有必需的依赖项和工具后,我执行以下命令:

RUN go get github.com/<my_org>/<my_package>

这里的包是一个私有仓库。

我已经添加了我的GitHub SSH密钥,以便从私有仓库克隆到Docker文件中:

ADD priv/id_rsa /root/.ssh/id_rsa
ADD priv/id_rsa.pub /root/.ssh/id_rsa.pub

然而,在go执行克隆repo的过程中,我遇到了一个错误:

# cd .; git clone https://github.com/<my_org>/<my_package> /gopath/src/github.com/<my_org>/<my_package>
Cloning into '/gopath/src/github.com/<my_org>/<my_package>'...
fatal: could not read Username for 'https://github.com': No such device or address
package github.com/<my_org>/<my_package>: exit status 128

为了调试问题,我从Dockerfile中运行以下命令:
RUN ssh-keyscan -t rsa github.com 2>&1 >> /root/.ssh/known_hosts

这告诉我存在一些问题。似乎私钥验证是正常的,但公钥出现了一些奇怪的情况。以下是完整的ssh-keyscan结果:

OpenSSH_6.0p1 Debian-4+deb7u2, OpenSSL 1.0.1e 11 Feb 2013
Pseudo-terminal will not be allocated because stdin is not a terminal.
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to github.com [192.30.252.129] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/id_rsa type 1
debug1: Checking blacklist file /usr/share/ssh/blacklist.RSA-2048
debug1: Checking blacklist file /etc/ssh/blacklist.RSA-2048
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: identity file /root/.ssh/id_dsa type -1
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version libssh-0.6.0
debug1: no match: libssh-0.6.0
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.0p1 Debian-4+deb7u2
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-sha1 none
debug1: kex: client->server aes128-ctr hmac-sha1 none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: RSA 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /root/.ssh/known_hosts:1
Warning: Permanently added the RSA host key for IP address '192.30.252.129' to the list of known hosts.
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /root/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: key_parse_private_pem: PEM_read_PrivateKey failed
debug1: read PEM private key done: type <unknown>
debug1: read_passphrase: can't open /dev/tty: No such device or address
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: No more authentication methods to try.
Permission denied (publickey).

我曾经尝试过在私钥/公钥上运行chmod 600和chmod 700,但这并没有帮助。
有什么线索吗?是否有人成功地在Debian中从Docker获取来自私有存储库的go get?

你是否以root身份运行go get命令?你应该使用用户账户来运行go get,并将你的密钥存储在/home/yourAccount中。 - VonC
检查权限和共享首选项,如https://dev59.com/8XjZa4cB1Zd3GeqPiMuc#19798820中所述,是否有帮助? - VonC
5个回答

15

经过一番试验,我找出了解决方法。但这并不是一个理想的解决方案,因为它需要安装SSH,并将私钥构建到容器中。此示例基于官方的Docker Golang镜像(Debian Wheezy):

与您的示例的主要区别在于,您需要使用git config命令强制使用SSH而不是默认的HTTPS。

FROM golang

RUN apt-get update && apt-get install -y ca-certificates git-core ssh

ADD keys/my_key_rsa /root/.ssh/id_rsa
RUN chmod 700 /root/.ssh/id_rsa
RUN echo "Host github.com\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config
RUN git config --global url.ssh://git@github.com/.insteadOf https://github.com/

ADD . /go/src/github.com/myaccount/myprivaterepo

RUN go get github.com/myaccount/myprivaterepo
RUN go install github.com/myaccount/myprivaterepo

11
我对这个解决方案的问题在于它需要将我的私有ssh密钥复制到源代码所在的目录中。有没有避免这种情况的方法? - nojo
同意nojo的说法,私钥不应该放在容器里面,毕竟它是“私有的”。 - Ualter Jr.
1
也许使用多阶段构建可以解决这个问题,因为凭证仅保存在构建阶段而不是最终镜像中。请参阅 https://docs.docker.com/develop/develop-images/multistage-build/ 了解更多详情。 - matt

13

go get 正在尝试使用 https,完全忽略 ssh。

您需要设置 ~/.netrc

ADD priv/.netrc /root/.netrc

netrc文件的格式如下:

machine github.com login github-username password github-password

ref:


感谢您的帮助。问题是,在我的Mac机器上,使用私有仓库时go get可以正常工作(身份验证顺利进行)。但是在我正在使用的Debian Docker镜像上出现了问题。此外,请注意,在go get之前就观察到了错误(ssh-keyscan输出显示存在错误,如我所发布的)。 - orcaman
@orcaman 读取PEM私钥完成:类型<未知>可能是一个损坏的私钥。 - OneOfOne
1
"go get 正试图使用 https,完全忽略 ssh。实际上,可以通过 RUN git config --global url."git@github.com:".insteadOf "https://github.com" 来解决这个问题。" - Eksapsy

7
在最新版本的golang(v1.11)中,现在有模块
引用来源如下:
“模块是一组相关的Go包,它们作为一个单元进行版本控制。通常情况下,一个版本控制仓库恰好对应一个模块。”
使用最新版本的golang将允许您拥有私有存储库中的依赖项。通过运行$ go mod vendor命令,将为所有外部依赖项在本地创建一个vendor目录。现在,确保您的Docker镜像具有Golang v1.11,您将使用以下内容更新您的Dockerfile:
WORKDIR /<your repository>

COPY . ./

6
详细说明OneOfOne关于~/.netrc的答案,这是我在Linux上使用Jenkins所做的事情:
FROM golang:1.6

ARG GITHUB_USER=$GITHUB_USER
ARG GITHUB_PASS=$GITHUB_PASS

# Copy local package files to the container's workspace.
ADD . /go/src/github.com/my-org/my-project
WORKDIR /go/src/github.com/my-org/my-project/

# Build application inside the container.
RUN echo "machine github.com\n\tlogin $GITHUB_USER\n\tpassword $GITHUB_PASS" >> ~/.netrc && \
    go get github.com/tools/godep && \
    go get github.com/onsi/ginkgo/ginkgo && \
    godep restore && \
    ginkgo -r --randomizeAllSpecs --randomizeSuites --failOnPending && \
    godep go install && \
    rm -f ~/.netrc

ENTRYPOINT /go/bin/my-project

EXPOSE 8080

Docker构建命令是:
docker build \
    --build-arg GITHUB_USER=xxxxx \
    --build-arg GITHUB_PASS=yyyyy \
    -t my-project .

两个ARG指令将--build-arg映射到Dockerfile中可用的位置。

RUN命令的第一行和最后一行创建并删除~/.netrc

在Jenkins中,我在构建命令中使用与git pull相同的凭证。

在此策略中,密码不会在docker构建过程中显示,并且不会保存在docker镜像的任何层中。请注意,在构建过程中会将gingko测试结果打印到控制台上。


2
这种策略存在两个问题:1、历史记录以明文形式存储你的用户名和密码。因此,任何能够访问你的电脑的人都可以运行“history”并查看你的凭据,无论是无意中还是有意的。2、在docker-compose中无法使用该策略。如果你这么做,就会暴露你的凭据,而且更糟糕的是,可能会意外提交文件中的更改。如果你想使用docker-compose,这真的是一个致命缺陷。 - Eksapsy
@Eksapsy - 不错的安全问题 - 每个用户应该根据自己的风险承受能力进行调整。Docker和GitHub在过去5年中取得了很大进展,并提供了几种安全替代方案,但我们在这些问题上还不够了解,只能让您开始着手处理。 - Steve Tarver
1
这个问题总的来说很烦人。似乎没有一个好的通用解决方案,这让人感到沮丧。Docker 应该在这个问题上更进一步,为我们提供一个好的解决方案。无论如何,尝试提供解决方案是做得不错的,对于一些人可能会起作用。但就我个人而言,我认为这太冒险了,特别是现在大多数人使用的 docker-compose,安全性令人担忧。 - Eksapsy
我认为这不是解决这个问题的好方法,你只能配置你的git使用“个人访问令牌”。 - Amirreza Saki

5

我在GitHub遇到了这个问题,并使用个人访问令牌进行了修复:

  1. 在Dockerfile中使用ARG来定义变量(输入)
  2. 使用GitHub个人访问令牌配置你的Git

GITHUB_PAT是GitHub个人访问令牌:

FROM golang:1.17 as builder

ARG GITHUB_PAT

WORKDIR /your-app
COPY go.mod .
COPY go.sum .
RUN git config --global url."https://${GITHUB_PAT}:x-oauth-basic@github.com/".insteadOf "https://github.com/"
RUN go mod download
COPY . .
RUN go build -ldflags '-w -s' -o ./out ./main.go


FROM golang:1.17

WORKDIR /app

COPY --from=builder /your-app/out ./
WORKDIR /app/

ENTRYPOINT [ "./out" ]

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