简体   繁体   English

如何从 docker-compose 中的已安装卷安装本地 pip package

[英]How to install local pip package from mounted volume in docker-compose

I am working on a project which has dependency on another project.我正在开发一个依赖于另一个项目的项目。

In live environment, the dependency is published and installed simply using pip install .在实时环境中,只需使用pip install即可发布和安装依赖项。 On my local environment, I'd like to be able to install the local dependency instead, using the pip install -e command.在我的本地环境中,我希望能够使用pip install -e命令安装本地依赖项。

The structure is as follow:结构如下:

- Home
--- Project1
----- docker-compose
----- Dockerfile
--- relaton-py

In this structure, Project1 has dependency on relaton-py, thus I'd like to "install" this dependency using the local relaton-py.在这个结构中,Project1 依赖于 relaton-py,因此我想使用本地的 relaton-py 来“安装”这个依赖项。

My docker-compose file looks like:我的 docker-compose 文件如下所示:

volumes:
      - .:/code
      - /Users/myuser/Dev/Projects/relaton-py:/relaton-py

while the Dockerfile looks like:而 Dockerfile 看起来像:

COPY requirements.txt /code/requirements.txt

RUN ["pip", "install", "-e", "relaton-py"]

WORKDIR /code

RUN ["pip", "install", "-r", "requirements.txt"]

# Copy the rest of the codebase
COPY . /code

When trying to spin un the environment, I get the following error:尝试旋转环境时,出现以下错误:

=> ERROR [local/web-precheck:latest 11/19] RUN ["pip", "install", "-e", "relaton-py"]                                                                                                                 1.2s
------
 > [local/web-precheck:latest 11/19] RUN ["pip", "install", "-e", "relaton-py"]:
#27 0.685 ERROR: relaton-py is not a valid editable requirement. It should either be a path to a local project or a VCS URL (beginning with bzr+http, bzr+https, bzr+ssh, bzr+sftp, bzr+ftp, bzr+lp, bzr+file, git+http, git+https, git+ssh, git+git, git+file, hg+file, hg+http, hg+https, hg+ssh, hg+static-http, svn+ssh, svn+http, svn+https, svn+svn, svn+file).
#27 0.889 WARNING: You are using pip version 22.0.4; however, version 22.1.2 is available.
#27 0.889 You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
------
failed to solve: rpc error: code = Unknown desc = executor failed running [pip install -e relaton-py]: exit code: 1

However, if I try not to install this local dependency in the Dockerfile, the entire environment spins up and I am able to access the container and install the package manually with the same command pip install -e relaton-py .但是,如果我尝试不在 Dockerfile 中安装此本地依赖项,整个环境就会启动,我可以使用相同的命令pip install -e relaton-py

Has anyone had to deal with this sort of thing already?有没有人不得不处理这种事情? Any idea on how to make Dockerfile recognise the files in the mounted volumes?关于如何使 Dockerfile 识别已安装卷中的文件的任何想法?

The volume is only bound when you run the container, the pip install -e relaton-py instead is happening when you build the image so the volume bind has not happened yet.卷仅在您运行容器时绑定,而pip install -e relaton-py在您构建映像时发生,因此卷绑定尚未发生。

I would suggest embedding relaton-py inside the container.我建议在容器中嵌入 relaton-py。

To do so you need to change the build context to include the relaton-py directory.为此,您需要更改构建上下文以包含 relaton-py 目录。 So in docker-compose.yml所以在docker-compose.yml

services:
  Project1:
    build:
      context: ../
      dockerfile: ./Project1/Dockerfile

And andjust your Dockerfile accordingly并相应地调整您的 Dockerfile

COPY ./relaton-py /relaton-py
RUN ["pip", "install", "-e", "/relaton-py"]

WORKDIR /code

COPY ./Project1/requirements.txt requirements.txt
RUN ["pip", "install", "-r", "requirements.txt"]

COPY ./Project1 .

This way the code will be available when you build the image.这样,当您构建映像时,代码将可用。

Then if you need your local code changes to happen in the container as well you can mount your local directory on top of the directory inside the container by doing:然后,如果您还需要在容器中进行本地代码更改,您可以通过执行以下操作将本地目录安装在容器内的目录之上:

volumes:
      - .:/code
      - ../relaton-py:/relaton-py

For simpler packages, it may be enough to just mount it directly into the site-packages .对于更简单的包,只需将其直接安装到site-packages就足够了。

In your example, you might want to try:在您的示例中,您可能想尝试:

  1. putting relaton-py (just the package in its source form, eg the Git repository) one directory above your Compose file, andrelaton-py (只是 package 的源代码形式,例如 Git 存储库)放在 Compose 文件上方的一个目录中,以及
  2. adding this line to docker-compose.yml under volumes :将此行添加到volumes下的docker-compose.yml
- ../relaton-py/relaton:/usr/local/lib/python3.10/site-packages/relaton:ro

Assuming you use Python 3.10, this should override whatever gets installed from PyPI during Docker image build with the directory from your host system, which would be mounted in read-only mode to avoid accidental changes.假设您使用 Python 3.10,这应该覆盖在 Docker 映像构建期间从 PyPI 安装的任何内容,使用主机系统中的目录,该目录将以只读模式安装以避免意外更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM