【发布时间】: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