如何使用pip作为docker build的一部分安装本地包?

Anj*_*Man 20 python pip docker

我有一个包,我想要构建一个docker镜像,它取决于我系统上的相邻包.

requirements.txt看起来像这样:

-e ../other_module
numpy==1.0.0
flask==0.12.5

当我打电话pip install -r requirements.txt给virtualenv时,这很好.但是,如果我在Dockerfile中调用它,例如:

ADD requirements.txt /app
RUN 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+

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

Cle*_*red 22

首先,您需要添加other_module到Docker镜像中.没有它,pip install命令将无法找到它.但是ADD根据文档,您无法访问Dockerfile目录之外的目录:

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

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

.
??? Dockerfile
??? requirements.txt
??? other_module
|   ??? modue_file.xyz
|   ??? another_module_file.xyz
Run Code Online (Sandbox Code Playgroud)

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

ADD /other_module /other_module
ADD requirements.txt /app
WORKDIR /app
RUN pip install -r requirements.txt
Run Code Online (Sandbox Code Playgroud)

WORKDIR命令将您带入,/app以便下一步,RUN pip install...将在/app目录中执行.而从应用程序的目录,你现在有目录../other_moduleavaliable

  • 因此,如果多个项目依赖于“other_module”,那么所有这些项目都需要将“../other_module”复制到“./other_module”,以便在构建 docker 映像时可用?是否有更好的方法(例如不涉及复制)?符号链接足以使其工作吗? (8认同)