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

前端之家收集整理的这篇文章主要介绍了如何使用pip作为docker build的一部分安装本地包?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

  1. -e ../other_module
  2. numpy==1.0.0
  3. flask==0.12.5

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

  1. ADD requirements.txt /app
  2. RUN pip install -r requirements.txt

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

../other_module应该是本地项目的路径或者以svn,git,hg或bzr开头的VCS url

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

最佳答案
首先,您需要将other_module添加到Docker镜像中.没有它,pip install命令将无法找到它.但是根据the documentation,您无法添加Dockerfile目录之外的目录:

The path must be inside the context of the build; you cannot ADD
../something /something,because the first step of a docker build is
to send the context directory (and subdirectories) to the docker
daemon.

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

  1. .
  2. ├── Dockerfile
  3. ├── requirements.txt
  4. ├── other_module
  5. | ├── modue_file.xyz
  6. | └── another_module_file.xyz

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

  1. ADD /other_module /other_module
  2. ADD requirements.txt /app
  3. WORKDIR /app
  4. RUN pip install -r requirements.txt

WORKDIR命令将您移动到/ app,因此下一步,RUN pip install …将在/ app目录中执行.从app-directory,你现在有了目录../ other_module avaliable

猜你在找的Docker相关文章