繁体   English   中英

ssh 中的密钥 Dockerfile 返回权限被拒绝(公钥)

[英]ssh key in Dockerfile returning Permission denied (publickey)

I'm trying to build a Docker image using DOCKER_BUILDKIT which involves cloning a private remote repository from GitLab, with the following lines of my Dockerfile being used for the git clone:

# Download public key for gitlab.com
RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
RUN --mount=type=ssh git clone git@gitlab.com:*name_of_repo* *download_location*

但是,当我使用以下命令运行 docker 构建命令时:

DOCKER_BUILDKIT=1 docker build --ssh default --tag test:local .

尝试执行 git 克隆时出现以下错误:

git@gitlab.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I've set up the ssh access successfully on the machine I'm trying to build this image on, and both the ssh -T git@gitlab.com and trying to clone the repository outside of the Docker build work just fine.

我环顾四周,但找不到任何可能导致此特定问题的信息 - 任何指针都非常感谢。

对于测试,请使用未加密的私有 SSH 密钥(这意味着您不必管理ssh-agent ,它仅用于加密的私有密钥密码短语缓存)

并使用ssh -Tv git@gitlab.com检查 SSH在哪里寻找您的密钥。

然后,在您的 Dockerfile 中,在git clone行之前添加:

ENV GIT_SSH_COMMAND='ssh -Tv'

当使用 SSH URL 执行git clone时,您将再次看到Docker/SSH 正在寻找的位置。

在这里建议了很多,然后缺少一些安装文件夹。

确保您有一个正在运行的 SSH 代理,并且您已向其中添加了您的私钥。

根据您的平台,命令可能会有所不同,但由于它被标记gitlab我会假设 Linux 是您的平台。

验证您有一个 SSH 代理运行echo $SSH_AUTH_SOCKecho $SSH_AGENT_SOCK如果两者都回显一个空字符串,您很可能没有运行代理。

要启动代理,您通常可以键入:

eval `ssh-agent`

接下来,您可以验证添加了哪些密钥(如果有):

ssh-add -l

如果您需要的密钥未列出,您可以添加它:

ssh-add /path/to/your/private-key

那你应该对go好。

更多信息: https://www.ssh.com/academy/ssh/agent

干杯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM