【问题标题】:when create docker image using Jenkins, id_rsa.pub key does not pass使用 Jenkins 创建 docker 镜像时,id_rsa.pub 键不通过
【发布时间】:2020-04-05 17:31:27
【问题描述】:

我正在尝试使用 Jenkins 构建 docker 映像文件。 Jenkins 文件将 id_rsa 和 id_rsa.pub 复制到 docker 文件,以 pip 安装 bitbucket 私有存储库。但是,我发现 id_rsa 密钥很好地保存在 docker 映像中,但 id_rsa.pub 密钥文件只是空的。我猜是因为 docker 图像显示无法运行 pip install bitbucket repos 的错误。

这是 Jenkins 文件

pipeline {
agent any
environment {
    imageUrl = "$ecsRegistry:$BUILD_NUMBER"
}
stages {
    stage("Docker Build") {
        steps {
            script {
                try {
                    // copy ssh key for install repo lib
                    env.SSH_PRIVATE_KEY = cat ~/.ssh/id_rsa
                    env.SSH_PUBLIC_KEY = cat ~/.ssh/id_rsa.pub
                } catch(Exception e) {
                    echo e
                }
            }
            sh "docker build -t $imageUrl --build-arg SSH_PRIVATE_KEY="${env.SSH_PRIVATE_KEY}" --build-arg SSH_PUBLIC_KEY="${env.SSH_PUBLIC_KEY}" ."
        }
    }
}
}

这是 docker 文件

FROM python:2.7.15-slim-jessie

# arguments
ARG SSH_PRIVATE_KEY
ARG SSH_PUBLIC_KEY

RUN apt-get update -y \
&& apt-get install apt-file -y \
&& apt-file update -y \
&& apt-get install -y python3-dev build-essential libmysqlclient-dev git libffi-dev \
&& pip install --upgrade pip

RUN mkdir -p ~/.ssh && chmod 0700 ~/.ssh

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

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

ADD service /app
WORKDIR /app

ADD ./service/requirements.txt /app/
RUN pip install -r requirements.txt

ADD ./service/manage.py /app/
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

EXPOSE 8000

这是错误日志

Collecting privaterepo

Cloning ssh://****@bitbucket.org/service/privaterepo.git (to revision v0.5.10) to /tmp/pip-install-apU7vk/privaterepo.git
Running command git clone -q 'ssh://****@bitbucket.org/service/privaterepo.git' /tmp/pip-install-apU7vk/privaterepo.git
key_load_public: invalid format
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
ERROR: Command errored out with exit status 128: git clone -q 'ssh://****@bitbucket.org/service/privaterepo.git' /tmp/pip-install-apU7vk/privaterepo.git Check the logs for full command output.


The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 1

但是,如果我 ssh 进入 docker 映像并运行 pip install ****@bitbucket.org/service/privaterepo.git,它会运行良好。当 docker 在构建映像时尝试运行 install private repo 时,它就会失败。

【问题讨论】:

  • 这种方法会将您的私钥泄漏到图像中,以后获得该图像的任何人都可以检索它。您应该在 Dockerfile 之外执行 git clone,例如在 Jenkinsfile 中的 checkout scm 指令中。

标签: python docker jenkins


【解决方案1】:

由于变量名中的拼写错误,您的公钥为空:SSH_PUBLIC_kEY 而不是SSH_PUBLIC_KEY(注意小写K)。不过没关系,因为作为客户端认证时不使用公钥,它只使用私钥(必须在服务器上安装公钥)。

这个Host key verification failed.表示你的known_hosts文件有问题,可能是扫描bitbucket.com但从bitbucket.org克隆,所以找不到服务器匹配的key。

【讨论】:

    猜你喜欢
    • 2017-11-04
    • 2019-10-18
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 2017-07-16
    • 2020-06-02
    相关资源
    最近更新 更多