【发布时间】: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图像中。 Enablebuildkitand use yourssh-agentduring the build.
标签: docker dockerfile