【问题标题】:How to install local packages using pip as part of a docker build?如何使用 pip 安装本地软件包作为 docker 构建的一部分?
【发布时间】:2017-11-26 06:27:41
【问题描述】:

我有一个包,我想将它构建到一个依赖于我系统上相邻包的 docker 映像中。

我的requirements.txt 看起来像这样:

-e ../other_module numpy==1.0.0 烧瓶==0.12.5

当我在 virtualenv 中调用 pip install -r requirements.txt 时,它可以正常工作。但是,如果我在 Dockerfile 中调用它,例如:

添加 requirements.txt /app 运行 pip install -r requirements.txt

并使用docker build . 运行我收到一条错误消息:

../other_module should either be a path to a local project or a VCS url beginning with svn+, git+, hg+, or bzr+

如果有的话,我在这里做错了什么?

【问题讨论】:

  • 你的 Docker 镜像中有 other_module 吗?
  • 你是否也将 ../other_module 添加到 docker 映像中?
  • @9000 @Cleared 我尝试使用类似COPY ../other_module /app 的方式复制它,但这会产生不同的错误:Forbidden path outside the build context

标签: python docker pip


【解决方案1】:

首先,您需要将other_module 添加到您的Docker 映像中。没有它,pip install 命令将无法找到它。但是根据the documentation,您不能ADD Dockerfile 目录之外的目录:

路径必须在构建的上下文中;你不能添加 ../something /something,因为 docker build 的第一步是 将上下文目录(和子目录)发送到 docker 守护进程。

因此,您必须将 other_module 目录移动到与 Dockerfile 相同的目录中,即您的结构应该类似于

.
├── Dockerfile
├── requirements.txt
├── other_module
|   ├── modue_file.xyz
|   └── another_module_file.xyz

然后将以下内容添加到 dockerfile:

ADD /other_module /other_module
ADD requirements.txt /app
WORKDIR /app
RUN pip install -r requirements.txt

WORKDIR 命令将您移动到/app,因此下一步,RUN pip install... 将在/app 目录中执行。从应用程序目录中,您现在拥有目录../other_module avaliable

【讨论】:

  • 我要补充一点的是,使用这种方法,不需要编辑他们的 requirements.txt 文件——在本地开发时,您可以运行 pip install -e ../other_module/ 来获取本地副本库,复制后在 Docker 文件中添加相同的命令即可。
  • 因此,如果多个项目依赖于other_module,那么所有这些项目都需要将../other_module 复制到./other_module 以便在构建docker 映像时可用?有没有更好的方法(例如不涉及复制)?符号链接是否足以使其正常工作?
猜你喜欢
  • 2016-09-23
  • 1970-01-01
  • 2017-06-25
  • 2021-09-16
  • 1970-01-01
  • 1970-01-01
  • 2021-06-15
  • 2011-11-05
相关资源
最近更新 更多