【发布时间】:2017-12-13 06:59:14
【问题描述】:
我正在尝试将存储库克隆到计算机上的映像中,在该计算机上,我成功地使用 SSH 克隆了 Docker 构建之外的存储库。但是,每次我尝试克隆存储库时,都会以以下借口被拒绝:
Cloning into 'my-repo'...
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
这是我的 Dockerfile:
FROM ubuntu as my-repo
ARG GIT_SSH_KEY
ARG GIT_SSH_PUBLIC_KEY
ARG KNOWN_HOSTS
RUN apt-get update
RUN apt-get install -y git ssh
# populate id_rsa files, populate known_hosts and config files, and manage permissions
RUN mkdir ~/.ssh/ && \
chmod 700 ~/.ssh && \
touch ~/.ssh/id_rsa && \
touch ~/.ssh/id_rsa.pub && \
chmod 600 ~/.ssh/id_rsa && \
chmod 600 ~/.ssh/id_rsa.pub && \
touch ~/.ssh/known_hosts && \
touch ~/.ssh/config && \
chmod 600 ~/.ssh/known_hosts && \
chmod 600 ~/.ssh/config && \
echo "${GIT_SSH_KEY}" > ~/.ssh/id_rsa && \
echo "${GIT_SSH_PUBLIC_KEY}" > ~/.ssh/id_rsa.pub && \
echo "${KNOWN_HOSTS}" > ~/.ssh/known_hosts && \
echo "Host bitbucket.example.com\n\tStrictHostKeyChecking no\n" > ~/.ssh/config && \
git clone ssh://git@bitbucket.example.com/my-repo.git && \
rm ~/.ssh/*
我已通过使用 echo 进行调试,验证传入的参数与我本地计算机上的那些字段的值相同。此外,在机器上运行 git clone ssh://git@bitbucket.example.com/my-repo.git 可以正常工作。
我遗漏了什么导致权限被拒绝?它将 publickey 声明为原因,但 id_rsa.pub 已正确填充。
编辑:这里是 docker 命令
docker build --build-arg GIT_SSH_KEY="$GIT_SSH_KEY" --build-arg GIT_SSH_PUBLIC_KEY="$GIT_SSH_PUBLIC_KEY" --build-arg KNOWN_HOSTS="$KNOWN_HOSTS" -f myRepoDockerfile -t myRepo .
我通过运行诸如
KNOWN_HOSTS='cat ~/.ssh/known_hosts'
【问题讨论】:
-
你的 SSH 密钥对有密码吗?
-
(a) 你的
docker build命令行到底是什么样的? (b) 您的公钥与此无关。您使用主机上的 private 密钥对存储在 bitbucket 上的 public 密钥进行身份验证。 (c) 你是如何验证容器中私钥文件的内容的? -
@John 不,当我在 docker 之外的本地机器上运行 git clone 时,它不会要求我输入密码,它会立即开始下载
-
@larsks a.) 更新了 OP b.) 我也将私钥导入了 docker build c.) 我通过在构建中运行 cat ~/.ssh/id_rsa 来调试和里面的内容和我机器上的一样
-
在构建环境中设置
GIT_SSH_COMMAND='ssh -v'会产生任何有用的信息吗?这将在克隆存储库时运行ssh -v,这可能会提供一些有用的诊断信息。如果您从RUN命令中删除git clone...和rm,然后在生成的映像中启动一个shell 并手动尝试会怎样?您是否 100% 确定您使用的是正确的密钥(例如,您确定主机上的成功不取决于您已将密钥加载到 ssh-agent 中这一事实)?