在 Dockerfile 中使用 SSH 进行 Git 克隆

2024-01-25

我正在尝试在 docker 映像中使用 ssh 克隆 git 存储库,并且我想在构建 dockerfile 时执行此操作。我可以使用 git clone https 以及用户名和密码来完成这项工作,但使用 ssh 会失败。我的 dockerfile 看起来像

# Install software 
RUN apt-get install -y git
# Make ssh dir
RUN mkdir -p /${APP_USER}/.ssh/

RUN apt-get update
RUN apt-get -qq -y install curl
RUN apt-get -qq -y install \
  openssh-client openssh-server
ADD id_rsa /${APP_USER}/.ssh/id_rsa

# Create known_hosts
RUN touch /${APP_USER}/.ssh/known_hosts
# Add bitbuckets key
RUN ssh-keyscan bitbucket.org >> /${APP_USER}/.ssh/known_hosts

# Pull the master branch 
RUN cd ${HOME} \
 && git clone ssh://[email protected] /cdn-cgi/l/email-protection...... \
 && ls -la


我添加了安装 git 的步骤,将 ssh 私钥复制到用户文件夹,将公钥放在 bitbucket 存储库中,添加了已知主机,但仍然出现以下错误

Host key verification failed.
fatal: Could not read from remote repository.

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



添加运行ssh -Tv [email protected] /cdn-cgi/l/email-protection检查是否有任何错误消息。

我怀疑例如 chmod 问题

RUN \
    chmod 700 $HOME/.ssh &&\
    chmod 600 $HOME/.ssh/id_rsa 

正如评论中所说,ssh-keyscan(如果你已经安装了 https://stackoverflow.com/a/32665960/6309)可能需要,以完成ssh/known_hosts文件。例如:

# Authorize SSH Host
RUN mkdir -p /root/.ssh && \
    chmod 0700 /root/.ssh && \
    ssh-keyscan example.com > /root/.ssh/known_hosts && \
    # Add the keys and set permissions
    echo "$PRIVATE_SSH_KEY" > /root/.ssh/id_rsa && \
    chmod 600 /root/.ssh/id_rsa
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Dockerfile 中使用 SSH 进行 Git 克隆 的相关文章

随机推荐