【问题标题】:Clone git repo from github with dockerfile使用 dockerfile 从 github 克隆 git repo
【发布时间】:2021-07-16 00:32:42
【问题描述】:

我正在尝试在安装 java、maven 后使用以下 docker 文件克隆 GitHub 存储库

FROM openjdk:8-jdk-alpine
LABEL WebAutomation Test <waaanjula@gmail.com>
RUN apk add --no-cache curl tar bash procps

# Downloading and installing Maven
# 1- Define a constant with the version of maven you want to install
ARG MAVEN_VERSION=3.8.1

# 3- Define the SHA key to validate the maven download
ARG SHA=0ec48eb515d93f8515d4abe465570dfded6fa13a3ceb9aab8031428442d9912ec20f066b2afbf56964ffe1ceb56f80321b50db73cf77a0e2445ad0211fb8e38d

# 4- Define the URL where maven can be downloaded from
ARG BASE_URL=https://apache.osuosl.org/maven/maven-3/${MAVEN_VERSION}/binaries

#5- Create the directories, download maven, validate the download, install it, remove downloaded file and set links
RUN mkdir -p /usr/share/maven /usr/share/maven/ref \
  && echo "Downlaoding maven" \
  && curl -fsSL -o /tmp/apache-maven.tar.gz ${BASE_URL}/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
  \
  && echo "Checking download hash" \
  && echo "${SHA}  /tmp/apache-maven.tar.gz" | sha512sum -c - \
  \
  && echo "Unziping maven" \
  && tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
  \
  && echo "Cleaning and setting links" \
  && rm -f /tmp/apache-maven.tar.gz \
  && ln -s /usr/share/maven/bin/mvn /usr/bin/mvn

# 6- Define environmental variables required by Maven, like Maven_Home directory and where the maven repo is located
ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2"

# # Install dependencies required to git clone.
RUN apk update && \
    apk add --update git && \
    apk add --update openssh

# 1. Create the SSH directory.
# 2. Populate the private key file.
# 3. Set the required permissions.
# 4. Add github to our list of known hosts for ssh.
RUN mkdir -p /root/.ssh/ 
ADD id_rsa /root/.ssh/id_rsa 
ADD id_rsa.pub /root/.ssh/id_rsa.pub

RUN chmod -R 700 /root/.ssh/ && \
    touch ~/.ssh/known_hosts && \
    ssh-keyscan -t rsa github.com >> /root/.ssh/known_hosts
    
RUN mkdir /usr/AutomationProject/ && \
    chmod -R 600 /usr/AutomationProject/  

# # Clone a repository (MatificWebAutomation project)
RUN git clone git@github.com:anjulaw/Selenium_Keyword_driven_Example.git /usr/AutomationProject

CMD [""]

我遇到了错误

"git@github.com:权限被拒绝(公钥)。 致命:无法从远程存储库中读取。 请确保您具有正确的访问权限并且存储库存在。”

这是我第一次使用 dockerfiles,但从我读过的内容(以及从工作配置中获取)我看不出为什么这不起作用。

我的 id_rsa 与我的 dockerfile 在同一个文件夹中,并且是我的本地密钥的副本,可以克隆这个 repo 没问题。

【问题讨论】:

  • 我很确定您的 ssh 密钥需要 600 个权限才能工作。我建议在没有 git clone 指令的情况下以交互方式运行容器并从那里进行故障排除
  • 请记住,任何拥有该图像的人都可以docker run the-image cat /root/.ssh/id_rsa 并取回私钥。我强烈建议在 Dockerfile 之外运行 git clone 操作(甚至可能包括存储库中的 Dockerfile)。
  • 克隆 git 项目时,它会询问“密码”密钥,有什么方法可以跳过或传递密钥
  • 不要将您的密钥复制到docker 图像中。 Enable buildkit and use your ssh-agent during the build.

标签: docker dockerfile


【解决方案1】:

您需要在本地项目文件夹中创建一个包含密钥的文件夹,将生成的已安装在远程存储库中的密钥传输到那里。

# SSH Keys
ADD .ssh/id_rsa /root/.ssh/id_rsa
ADD .ssh/id_rsa.pub /root/.ssh/id_rsa.pub

RUN chmod 600 /root/.ssh/id_rsa &&\
    chmod 600 /root/.ssh/id_rsa.pub

原来每次构建之后都会生成密钥,也就是说每次构建这个Dockerfile之后,你都得重新配置远程仓库上的密钥?

【讨论】:

  • 克隆 git 项目时,它会询问“密码”密钥,有什么方法可以跳过或传递密钥
  • 要调试,在RUN cat /root/.ssh/id_rsa.pub 之后添加,并在设置配置文件中使用远程(Github 或 Bitbucket)ssh 密钥检查此打印密钥?。
  • 生成没有密码的 ssh 密钥后解决
猜你喜欢
  • 1970-01-01
  • 2013-11-07
  • 2022-11-02
  • 2020-11-24
  • 2012-10-08
  • 2014-09-03
  • 1970-01-01
  • 2013-08-07
相关资源
最近更新 更多