【问题标题】:Using git clone "ssh://" within Dockerfile在 Dockerfile 中使用 git clone "ssh://"
【发布时间】:2020-03-12 08:44:06
【问题描述】:

我正在尝试从 Dockerfile 中的 Gerrit 克隆公司存储库。

我没有问题将我的私有 SSH 密钥提供给映像而不将它们保留在那里(使用多阶段构建)。

我的问题是 Gerrit 上的公司 repo 需要通过 ssh 进行 git clone:

git clone "ssh://usr@gerrit.com:port/path/to/repo

我正在尝试通过 buildKit 实验来做到这一点。 Dockerfile中的运行命令:

RUN --mount=type=ssh git clone usr@gerrit.com:port/path/to/repo /image/path

但这并没有告诉 git 使用 ssh,所以它只会使用它的标准协议和端口。

在 Dockerfile 中执行此操作会产生以下错误

#14 0.659 Cloning into '/image/path'...                                                                                                                                                                         
#14 0.890 Warning: Permanently added the ECDSA host key for IP address 'xxx.xxx.xxx.xxx' to the list of known hosts.
#14 6.740 Permission denied, please try again.
#14 6.774 Received disconnect from xxx.xxx.xxx.xxx port 22:2: Too many authentication failures
#14 6.774 Disconnected from xxx.xxx.xxx.xxx port 22
#14 6.774 fatal: Could not read from remote repository.
#14 6.774 
#14 6.774 Please make sure you have the correct access rights
#14 6.774 and the repository exists.

该错误清楚地表明它尝试使用端口 22,尽管我另有说明。

如果我使用RUN git clone "ssh://usr@gerrit.com:port/path/to/repo",它表明它找不到远程仓库。 此命令仅在 Dockerfile 中失败,因为这是我公司克隆存储库的标准方式。

完整的 Dockerfile 在这里:

# syntax=docker/dockerfile:experimental
# Using multistage build, where intermediate stage is used for cloning private repo from gerrit
FROM ubuntu as intermediate

# install git
RUN apt-get update
RUN apt-get install -y git openssh-client

# add credentials on build
ARG SSH_PRIVATE_KEY
RUN mkdir /root/.ssh/
RUN echo "${SSH_PRIVATE_KEY}" > /root/.ssh/id_rsa && chmod 400 /root/.ssh/id_rsa

# Add gerrit to known hosts
RUN touch /root/.ssh/known_hosts
RUN ssh-keyscan gerrit.com >> /root/.ssh/known_hosts

# Clone our repo from gerrit
RUN --mount=type=ssh git clone usr@gerrit.com:port/path/to/repo /image/path

# Actual build stage here
FROM ubuntu:18.04
...

Dockerfile 中使用 git clone 时,还有其他方法可以显式使用 ssh 连接吗?

【问题讨论】:

  • 您是否需要代理或不同的 DNS 才能访问 repo 服务器?
  • 我在正常克隆时从不这样做,所以我想没有。

标签: git docker ubuntu ssh gerrit


【解决方案1】:

使用ssh时,需要确保.ssh目录和private keys的权限设置正确

设置/root/.ssh目录的权限为700

/root/.ssh/id_rsa Dockerfile 中的 600 的私钥

chmod 700 /root/.ssh && \
chmod 600 /root/.ssh/id_rsa

另外,请确保您的 ssh 密钥设置正确。在继续之前在本地测试它们。

【讨论】:

  • 不起作用,立即断开连接 - 然后它抱怨没有正确的访问权限。同样,在 dockerfile 之外进行本地克隆非常有效。
  • 这是你在本地使用的命令吗git clone ssh://usr@gerrit.com:29418/path/to/repo然后在Dockerfile中试试同样的语法RUN git clone ssh://usr@gerrit.com:29418/path/to/repo
【解决方案2】:

您似乎使用了错误的端口,Gerrit 使用 29418 作为 ssh 连接的端口号,而不是默认的 ssh 端口 22。

你声明你指定了端口,你是怎么做到的?您是否尝试过在 Dockerfile 中硬编码正确的端口号?

【讨论】:

  • 是的,我在 git clone 命令中声明了端口 29418:git clone "ssh://usr@gerrit.com:29418/path/to/repo 当我在问题中写 port 时,暗示它是用于 Gerrit 的端口
  • 但是正如问题中的错误所暗示的那样,它仍然使用端口 22 - 还有其他方法可以指定端口号吗?
  • 你可以使用ssh -p 29418
  • 哦,看起来你使用了两个 ssh 连接,一个用于 gerrit,一个用于运行命令。
猜你喜欢
  • 2020-04-18
  • 2012-08-04
  • 1970-01-01
  • 2012-08-24
  • 1970-01-01
  • 1970-01-01
  • 2018-10-19
  • 2012-05-25
  • 2018-08-29
相关资源
最近更新 更多