ssh-add in docker - 无法打开与您的身份验证代理的连接

ssh-add in docker - Could not open a connection to your authentication agent

我正在尝试为我的 Python 烧瓶 API 创建一个 docker 图像。

我需要 git 来安装依赖项,我已经在 docker 中安装了几次 git。但是在这里,我无法理解我做错了什么。

与docker:

FROM python:3.6-slim

ARG ssh_prv_key
ARG ssh_pub_key

RUN apt-get update && \
    apt-get install -y openssh-server &&\
    apt-get install -y git

# Authorize SSH Host
RUN mkdir -p /root/.ssh && \
    chmod 0700 /root/.ssh && \
    ssh-keyscan github.com > /root/.ssh/known_hosts

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


RUN eval "$(ssh-agent -s)"
RUN ssh-add /root/.ssh/id_rsa

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY requirements.txt /usr/src/app/
RUN pip3 install --no-cache-dir -r requirements.txt

# Remove SSH keys
RUN rm -rf /root/.ssh/

COPY ./my_api /usr/src/app

# Expose the Flask port
EXPOSE 5000

CMD [ "python", "./app.py" ]

我执行命令:

docker build --build-arg ssh_prv_key=.keys/id_rsa --build-arg ssh_pub_key=.keys/id_rsa.pub -t my-api -f Dockerfile . 

这给我以下错误:

Step 7/16 : RUN eval "$(ssh-agent -s)"
 ---> Running in be450cc39533
Agent pid 9
Removing intermediate container be450cc39533
 ---> fb101226dc5f
Step 8/16 : RUN ssh-add /root/.ssh/id_rsa
 ---> Running in 4288e93db584
Could not open a connection to your authentication agent.
The command '/bin/sh -c ssh-add /root/.ssh/id_rsa' returned a non-zero code: 2

ssh-agent 的 eval 函数检索到一个 PID,但我无法连接到它。

已解决

我终于发现我做错了什么。首先,我的构建参数不正确。正确的 docker build 命令如下:

docker build --build-arg ssh_prv_key="$(cat .keys/id_rsa)" --build-arg ssh_pub_key="$(cat .keys/id_rsa.pub)" -t my-api -f Dockerfile . 

此外,我不知道为什么,git 正确处理我的 ssh 密钥而不使用

RUN eval "$(ssh-agent -s)"
RUN ssh-add /root/.ssh/id_rsa  

上述命令导致无法连接到您的代理错误。

那么,正确的文件是

FROM python:3.6-slim

ARG ssh_prv_key
ARG ssh_pub_key

RUN apt-get update && \
    apt-get install -y git

# Authorize SSH Host
RUN mkdir -p /root/.ssh && \
    chmod 0700 /root/.ssh && \
    ssh-keyscan github.com > /root/.ssh/known_hosts

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


RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY requirements.txt /usr/src/app/
RUN pip3 install --no-cache-dir -r requirements.txt

# Remove SSH keys
RUN rm -rf /root/.ssh/

COPY ./my_api /usr/src/app

# Expose the Flask port
EXPOSE 5000

CMD [ "python", "./app.py" ]

我认为问题与您容器中的 ssh 配置有关,Ubuntu 中的默认 ssh 策略是拒绝 root 远程登录。

要启用它,请将以下行添加到您的 Dockerfile。

RUN echo "PermitRootLogin yes" >> /etc/ssh/sshd_config

此行编辑 /etc/ssh/sshd_config 文件以允许 root 登录,但您需要重新启动 sshd 服务,为此,您还必须在 Dockerfile 中添加以下行。

RUN systemctl restart sshd

此外,如果您信任该证书,只需将 -K 标志添加到 ssh-add。

RUN ssh-add -k /root/.ssh/id_rsa

使用 -k 选项将密钥加载到代理或从代理删除密钥时,仅处理普通私钥并跳过证书。

希望对您有所帮助。
最好的问候,

而不是写这些命令

RUN eval "$(ssh-agent -s)"
RUN ssh-add /root/.ssh/id_rsa  
RUN pip3 install --no-cache-dir -r requirements.txt

使用不同的 运行 语句,在单层中执行它们,即:

RUN eval "$(ssh-agent -s)" && \
    ssh-add /root/.ssh/id_rsa && \
    pip3 install --no-cache-dir -r requirements.txt

试过了,没有任何问题。