我正在尝试将一个存储库克隆到一台机器上的图像中,我可以成功地使用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/*
我已经验证传入的Arguments通过使用echos进行调试,使其与本地计算机上的那些字段的值相同。此外,运行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'
答案 0 :(得分:0)
事实证明,我的密钥对有一个密码。它不会问我在docker容器之外的密码,而是在docker容器内。这样做的原因是我的ssh-key
已经解锁并通过我的发布内容ssh-agent
加载到ssh-add
(我可以看到ssh-add -l
)。
如果ssh-add -l
没有显示任何结果,则仍有可能存在与该密钥相关联的密码。这就是我的情况。最好的方法是检查rsa_id private key
文件(详情如下)。
在构建期间,由于没有提供密码,它只是立即失败git pull但没有给出详细的错误消息。一旦我生成id_rsa keypair
没有相关密码并使用它,我就能克隆到docker构建中。
要检查密码保护是否有问题,您可以比较rsa_id private key
个文件,受密码保护的文件如下所示:
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,2895AF2DB67AF43E94DF3C097C69B693
YUtMfSyL4xna /* lots of numbers and letters */
-----END RSA PRIVATE KEY-----
虽然那些没有保护的人缺少包含Proc-Type的第2到4行:4,ENCRYPTED等等
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAK /* lots of letters and numbers */
-----END RSA PRIVATE KEY-----