【发布时间】: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