加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 03:43:41 所属栏目:安全 来源:网络整理
导读:我有一个包,我想要构建一个docker镜像,这取决于我系统上的相邻包. 我的requirements.txt看起来像这样: -e ../other_modulenumpy==1.0.0flask==0.12.5 当我在virtualenv中调用pip install -r requirements.txt时,这很好用.但是,如果我在Dockerfile中调用它,

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

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

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

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

ADD requirements.txt /app
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相同的目录中,即您的结构应该类似于

.
├── 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目录中执行.从app-directory,你现在有了目录../ other_module avaliable

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读