我正在尝试在conda env create
期间从BitBucket pip install(通过docker build
命令)自定义Python包命令,无需输入 SSH 密码/密码。这个问题类似于this other question ,但有所不同,因为错误发生在docker build
期间命令。
conda env create
的environment.yml 文件命令(在 docker build
期间)看起来像这样:
name: my_app
channels:
- defaults
dependencies:
- pip=21.2.2
- python=3.8.11
- pip:
- git+ssh://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="06616f7246646f726473656d637228697461" rel="noreferrer noopener nofollow">[email protected]</a>/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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b4d3ddc0f4d6ddc0d6c1d7dfd1c09adbc6d3" rel="noreferrer noopener nofollow">[email protected]</a>:my_org/my_package_repo.git /tmp/my_package_repo
--mount
命令在 /tmp
中创建存储库的本地副本目录。所以现在condaenvironment.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_BUILDKIT=1
在docker build
前面命令强制 Docker 使用 Docker Buildkit 进行构建,这是 RUN --mount=type=ssh git clone
所必需的Dockerfile 中的命令。这个解决方案现在对我有用。这并不完全是下面答案中的内容,所以我想我应该与社区分享。我会将指向我这个方向的答案标记为正确答案。
最佳答案
SSH 需要访问您的 RSA 私钥才能进行身份验证。您的私钥不会与 Docker 镜像内运行的任何命令共享。 Docker 有一个内置功能可以解决这个问题,记录在案 here .
本质上,您想要做的是将 PUBLIC
key 下载到 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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="07606e7347606e736f72652964686a" rel="noreferrer noopener nofollow">[email protected]</a>:myorg/myproject.git myproject
此运行命令可以是其他命令,例如安装 Pythin 软件包的命令。
您使用 -ssh
选项构建 Docker 镜像:
docker build --ssh default .
关于python - 使用 SSH 从 BitBucket pip 安装自定义包,无需在 Docker 构建期间输入 SSH 密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70160034/