简体   繁体   English

docker-compose 如何从另一个项目访问模块?

[英]docker-compose how to access module from another project?

I have 2 projects and i want project two to access functions in project one, how to do that?我有 2 个项目,我希望项目 2 访问项目一中的功能,该怎么做?

this is are my simple files :)这是我的简单文件:)

project项目

root
---one
------main.py
---two
------main.py

docker-compose.yml docker-compose.yml

services:
  one:
    build: ./one
  two:
    build: ./two

two/main.py两个/main.py

from one.main import myfun

myfun()

error from docker logs来自 docker 日志的错误

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    from one.main import myfun
ModuleNotFoundError: No module named 'one'

Cheers :)干杯:)

One container can't directly access or import code in another container, any more than if the two Python applications were running on physically separate machines.一个容器不能直接访问或导入另一个容器中的代码,就像两个 Python 应用程序在物理上分开的机器上运行一样。

Start by reading through the Python Packaging User Guide , and in particular, the guide on Packaging and distributing projects .首先通读Python Packaging User Guide ,特别是关于Packaging and distributing projects的指南。 You should treat your one application as an ordinary Python library;你应该把你的one应用程序当作一个普通的 Python 库; it should have a setup.py script and so on.它应该有一个setup.py脚本等等。

If one is runnable as an application in its own right, then one/Dockerfile can be extremely straightforward:如果one应用程序本身可以作为应用程序运行,那么one/Dockerfile可以非常简单:

FROM python:3.8
WORKDIR /app
COPY . ./
RUN pip install .
CMD some_console_script

There are a couple of ways to get this library into the application two .有几种方法可以将此库添加到应用程序two One is to run, outside of Docker:一种是在 Docker 之外运行:

cd ../one
python setup.py bdist_wheel
mv dist/one-*.whl ../two/one.whl
cd ../two
docker build -t me/two .

That Dockerfile can then manually install the built wheel file:然后该 Dockerfile 可以手动安装构建的轮文件:

FROM python:3.8
WORKDIR /app
COPY one.whl .
RUN pip install ./one.whl
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . ./
CMD ["./two.py"]

You can also use a multi-stage build to do this in one command, using a fully isolated environment.您还可以使用多阶段构建在一个命令中使用完全隔离的环境执行此操作。 The Dockerfile should be in the root directory in your diagram; Dockerfile 应该位于图表的root目录中; it can be somewhere else, but you will need to use the root path in the docker build command and all COPY paths will be relative to root .它可以在其他地方,但您需要在docker build命令中使用root路径,并且所有COPY路径都将相对于root

FROM python:3.8 AS build-one
WORKDIR /one
COPY one ./
RUN python setup.py bdist_wheel \
 && mv dist/one-*.whl one.whl

FROM python:3.8
WORKDIR /app
COPY --from=build-one /one/one.whl .
RUN pip install ./one.whl
COPY two/requirements.txt ./
# and repeating the rest of the "just build `two`" Dockerfile

I found the solution after some search and David Maze help经过一番搜索和 David Maze 的帮助,我找到了解决方案

I had to change docker-compose file, Dockerfile for project two and move it to main directory.我不得不更改 docker-compose 文件,项目two Dockerfile 并将其移动到主目录。

new docker-compose.yml新的 docker-compose.yml

version: '3'
services:
  one:
    build: ./one
  two:
    build:
      context: .
      dockerfile: Dockerfile-two

new Dockerfile-two新的 Dockerfile-二

FROM python:3.6
WORKDIR /two

COPY /two /two/
COPY /one /two/one/

COPY /two/requirements.txt .
RUN pip install -r requirements.txt

CMD ["python", "/two/main.py"]

It has to be in the main root to have access to all folders.它必须在主根目录中才能访问所有文件夹。 Cheers :)干杯:)

暂无
暂无

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

相关问题 在docker-compose烧瓶中构建项目期间访问ssh密钥 - Access ssh keys during docker-compose build in flask project 无法从 docker-compose 的另一个容器访问 api 的 docker 容器 - docker container of api not accessible from another container of docker-compose 如何使用 docker-compose 设置一个容器以允许它的整个卷访问另一个容器 - how to set one container to allow it's entire volume access to another container using docker-compose 从主机访问 mariadb docker-compose 容器 - Access mariadb docker-compose container from host-machine 如何在 Pycharm(或其他 Jetbrains 项目)中使用 docker-compose 运行 Colima - How to run Colima with docker-compose in Pycharm (or other Jetbrains project) 使用单独的 docker-compose yaml 时,如何从 PyMongo 访问 MongoDB? - How to access MongoDB from PyMongo when using separate docker-compose yaml's? Docker-compose:如何使用相同的网络地址从容器和主机访问 Localstack 资源 - Docker-compose: How to access Localstack resources both from container and host, using same network address 如何访问使用 docker-compose 设置的 postgresql 数据库? - How do I access a postgresql DB that was setup with docker-compose? 如何从Django源文件夹中分离docker和docker-compose - How to separate docker and docker-compose from Django source folder 无法在没有模块的情况下启动 docker-compose &amp; failes - Cant start docker-compose & failes on No module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM