[英]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.