【问题标题】:pip install custom package from BitBucket with SSH without entering SSH password during Docker build在 Docker 构建期间使用 SSH 从 BitBucket 安装自定义包,无需输入 SSH 密码
【发布时间】:2021-11-30 05:25:05
【问题描述】:

我正在尝试在 docker build 命令期间从 BitBucket 中 pip 安装(通过 conda env create 命令)自定义 python 包,而无需输入 SSH 密码/密码。此问题与this other question 类似,但不同之处在于错误发生在docker build 命令期间。

conda env create 命令(在docker build 期间)的 environment.yml 文件如下所示:

name: my_app
channels:
  - defaults
dependencies:
  - pip=21.2.2
  - python=3.8.11
  - pip:
    - git+ssh://git@bitbucket.org/my_org/my_package_repo.git
    - pandas==1.2.5
    - python-dotenv==0.19.0
    - xlrd==2.0.1
prefix: /usr/local/anaconda3/envs/my_app

当docker build 尝试在 Docker 映像中构建 conda 环境时,我收到此错误:

Installing pip dependencies: ...working... Pip subprocess error:
  Running command git clone -q 'ssh://****@bitbucket.org/my_org/my_package_repo.git' /tmp/pip-req-build-3t5mkmnw
  Host key verification failed.
  fatal: Could not read from remote repository.

  Please make sure you have the correct access rights
  and the repository exists.
WARNING: Discarding git+ssh://****@bitbucket.org/my_org/my_package_repo.git. Command errored out with exit status 128: git clone -q 'ssh://****@bitbucket.org/my_org/my_package_repo.git' /tmp/pip-req-build-3t5mkmnw Check the logs for full command output.
ERROR: Command errored out with exit status 128: git clone -q 'ssh://****@bitbucket.org/my_org/my_package_repo.git' /tmp/pip-req-build-3t5mkmnw Check the logs for full command output.

Ran pip subprocess with arguments:
['/opt/conda/envs/work_content/bin/python', '-m', 'pip', 'install', '-U', '-r', '/tmp/condaenv.9p_rq9_h.requirements.txt']
Pip subprocess output:
Collecting git+ssh://****@bitbucket.org/my_org/my_package_repo.git (from -r /tmp/condaenv.9p_rq9_h.requirements.txt (line 3))
  Cloning ssh://****@bitbucket.org/my_org/my_package_repo.git to ./pip-req-build-3t5mkmnw

failed

CondaEnvException: Pip failed

当我在终端中并按照referenced StackOverflow question 中的答案操作时,此远程/自定义包安装成功。但是,当我在运行 docker build 命令之前尝试相同的事情时,我得到了上面的错误。我假设这是因为 Docker 正在构建一个全新的操作系统映像,并且它不再具有我在终端中提供的 SSH RSA 密码。在构建过程中不提供密码短语的情况下如何解决此错误?

更新

根据当前答案之一中的建议,我已将Dockerfile 修改为如下所示:

# syntax=docker/dockerfile:experimental
FROM continuumio/miniconda3:4.10.3
...
RUN apt-get install -y openssh-client git
RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan bitbucket.org >> ~/.ssh/known_hosts
RUN --mount=type=ssh git clone git@bitbucket.org:my_org/my_package_repo.git /tmp/my_package_repo

--mount 命令在/tmp 目录中创建存储库的本地副本。因此,现在需要将 conda environment.yml 文件从该本地目录更改为 pip install,如下所示:

name: my_app
channels:
  - defaults
dependencies:
  - pip=21.2.2
  - python=3.8.11
  - pip:
    - /tmp/my_package_repo
    - pandas==1.2.5
    - python-dotenv==0.19.0
    - xlrd==2.0.1
prefix: /usr/local/anaconda3/envs/my_app

...我正在使用以下命令运行docker build 进程:

eval $(ssh-agent); ssh-add ~/.ssh/id_rsa
DOCKER_BUILDKIT=1 docker build --ssh default .

上面的eval 命令是在 Docker 构建开始之前手动输入我的 SSH 密码。 docker build 命令前面的 DOCKER_BUILDKIT=1 强制 Docker 使用 Docker Buildkit 构建,这是 Dockerfile 中的 RUN --mount=type=ssh git clone 命令所必需的。这个解决方案现在对我有用。这不完全是下面答案中的内容,所以我想我会与社区分享。我会将指出我这个方向的答案标记为正确答案。

【问题讨论】:

  • 您可以使用.netrc 文件在您的docker 容器gnu.org/software/inetutils/manual/html_node/… 中进行身份验证
  • .netrc 文件似乎很有希望。我应该如何设置它以与 Docker 和 bitbucket 一起使用?
  • 使用.netrc 比使用 ssh 身份验证更安全,因为您可以创建授予特定用途的令牌。您可以查看support.atlassian.com/bitbucket-cloud/docs/…,但 github 和 gitlab 也允许创建令牌。然后你用你的令牌信息创建你的.netrc 并通过 https:/ 不使用 ssh 连接到你的仓库

标签: python docker ssh pip bitbucket


【解决方案1】:

SSH 需要访问您的私有 RSA 密钥以进行身份​​验证。您的私钥不会与在 Docker 映像中运行的任何命令共享。 Docker 有一个内置特性可以解决这个问题,文档在 here.

基本上你想要做的是将你的PUBLIC 密钥下载到 Docker 映像:

# Download public key for github.com
RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts

定义一个类型为 ssh 的挂载,以便 Docker 引擎转发 SSH 代理连接。

RUN --mount=type=ssh git clone git@github.com:myorg/myproject.git myproject

这个运行命令可以是别的东西,例如安装你的 Pythin 包的东西。

您使用 -ssh 选项构建您的 docker 映像:

docker build --ssh default .

【讨论】:

  • 这个答案是在呼唤 GitHub,BitBucket 也一样吗?
  • 拥有 GitHub 或 BitBucket 并不重要。两者的 SSH 身份验证相同。此外,该行是从 Docker 文档中复制的。这只是一个例子,因为你不刮你的Dockerfile。
  • 我能够根据此解决方案中提供的提示找出解决方案。请参阅问题中的更新以获取基于此答案的最终解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-25
相关资源
最近更新 更多