关于git:在docker文件中为ssh-agent添加私钥

Add private key to ssh-agent in docker file

我正在尝试为Angular CLI项目编写Docker文件,但我有一个外部依赖项,它是BitBucket上的私有repo,所以我需要传递ssh密钥。我正在尝试使用--build-arg传递ssh密钥。

现在的问题是,它没有向ssh代理添加这些密钥,而是要求输入密码。

我正在使用此命令运行docker build -t ng-2-docker/client --build-arg ssh_prv_key="$(cat ~/.ssh/id_rsa)" --build-arg ssh_pub_key="$(cat ~/.ssh/id_rsa)" .

这是我的档案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
ARG ssh_prv_key
ARG ssh_pub_key

# Use an official Node runtime as a parent image
FROM node:8.9.4

# Specify working directory in docker container
WORKDIR /app

# Authorize SSH Host
RUN mkdir -p /ssh/
RUN chmod 0700 /ssh

# Add the keys and set permissions
RUN echo"$ssh_prv_key"> /ssh/id_rsa && echo"$ssh_pub_key"> /ssh/id_rsa.pub && chmod 600 /ssh/id_rsa && chmod 600 /ssh/id_rsa.pub

# add bitbucket to known hosts
RUN ssh-keyscan bitbucket.org > /ssh/known_hosts

# Copy SSH key to temp folder to pull new code
# ADD ~/.ssh/id_rsa /tmp/
# RUN ssh-agent /tmp
RUN ls -la /ssh

# check if ssh agent is running or not, if not, run
RUN eval `ssh-agent -s` && ssh-add /ssh/id_rsa

# Copy local files into the containers working directory
COPY package.json /app

# Install dependencies inside container
RUN npm i

# Copy local files into the containers working directory
COPY . /app

# Execute Process
CMD ["npm","docker:rogers:local"]

# Remove ssh key from temp
# RUN rm /tmp/id_rsa
RUN rm -rf /ssh

# expose port
EXPOSE 4200

这里是输出,如果我运行上面提到的命令。

enter image description here


Done this already, and my key is passphrase free right now but it's still asking

然后…如果您没有与您的私钥关联的密码短语,您应该去掉dockerfile行:

1
2
# check if ssh agent is running or not, if not, run
RUN eval `ssh-agent -s` && ssh-add /ssh/id_rsa

如果不需要记住/缓存密码短语,则不需要ssh代理。


从您的屏幕截图来看,git ssh客户机没有请求您的bitback密码。您的私钥文件是用密码短语加密的。要使用私钥,ssh需要密码短语。

一个选项是从私钥中删除密码短语。您可以使用ssh-keygen编辑您的私钥:

1
$ ssh-keygen -p

ssh keygen的源