【发布时间】:2020-04-17 23:14:45
【问题描述】:
我正在尝试克隆其中包含子模块的存储库。主仓库可以很好地克隆,但是当我在 dockerfile 中执行 git submodule update --init --recursive 时,子模块会抛出错误。
fatal: clone of 'git@github.com:jkeys089/lua-resty-hmac.git' into submodule path '/tmp/third-party/lua-resty-hmac' failed
Failed to clone 'third-party/lua-resty-hmac'. Retry scheduled
Cloning into '/tmp/third-party/lua-resty-jwt'...
load pubkey "/root/.ssh/id_rsa": invalid format
Warning: Permanently added the RSA host key for IP address '140.82.118.3' to the list of known hosts.
Load key "/root/.ssh/id_rsa": invalid format
git@github.com: Permission denied (publickey).
在图片中我有这个
# authorise ssh host
RUN mkdir /root/.ssh/ \
&& chmod 700 /root/.ssh \
&& ssh-keyscan github.com > /root/.ssh/known_hosts
# add key and set permission
RUN echo "${SSH_PRIVATE_KEY}" >> /root/.ssh/id_rsa \
&& echo "$ssh_pub_key" > /root/.ssh/id_rsa.pub \
&& chmod 600 /root/.ssh/id_rsa.pub \
&& chmod 600 /root/.ssh/id_rsa
我无法控制子模块。我不确定是否可以从 git@github.com 更改为 https 以获取子模块。
我什至尝试过使用GITHUB_TOKEN 路由
# start up git and clone
RUN git config --global url."https://${GITHUB_TOKEN}@github.com/".insteadOf "https://github.com/" \
&& git clone https://github.com/GluuFederation/gluu-gateway.git /tmp \
&& cd /tmp/ \
&& git submodule update --init --recursive
下面是构建命令的一部分。
build --build-arg GITHUB_TOKEN=${GITHUB_TOKEN} --build-arg SSH_PRIVATE_KEY="$(cat ~/.ssh/id_rsa)" --build-arg ssh_pub_key="$(cat ~/.ssh/id_rsa.pub)"
请帮忙解决这个问题。这非常令人沮丧。 :(
【问题讨论】:
-
您作为
SSH_PRIVATE_KEY传递的文件是公钥。请注意,这样做会损害您的密钥对,因为任何获取映像的人都可以轻松提取它,并且在 Dockerfile 中运行git命令还有其他几个缺点;在运行docker build之前,我会在主机上进行设置(克隆存储库,查看我想要构建的特定分支,...)。 -
您可以使用相同的
insteadOf技巧将git@github.com:...更改为 https URL,但请参阅 David Maze 的评论。 -
我的想法是,如果这样可行,我可以使用多阶段构建来避免在构建历史中留下私钥痕迹。
-
我已经更新了问题 - 关于构建命令中的 pub 键。我仍然遇到同样的错误。
-
你的私钥第一行的文本(破折号之间的部分)是什么,你在容器中运行的是什么操作系统?
标签: git docker github ssh dockerfile