[英]Run python file outside Docker container that imports a python file outside Docker container
So I have a simple Dockerfile:所以我有一个简单的 Dockerfile:
FROM ubuntu:latest
WORKDIR /home
RUN apt-get -y update
RUN apt install -y software-properties-common
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt install -y python3
It runs good with:它运行良好:
sudo docker build -t sample-image .
First I run my docker:首先我运行我的 docker:
sudo docker run -t -d --name mycontainername sample-image:latest
If I run:如果我运行:
sudo docker exec -i mycontainername python3 < helloworld.py
It works fine:它工作正常:
hello from docker码头工人你好
And then, I want to run a script a.py that imports a class_b from b.py.然后,我想运行一个从 b.py 导入 class_b 的脚本 a.py。 Note that a.py, b.py and helloworld.py are only in mi local computer, so they does not exist inside the container .注意 a.py、b.py 和 helloworld.py只存在于 mi 本地计算机中,因此它们不存在于容器中。 a.py, b.py and helloworld.py are in the same folder a.py、b.py 和 helloworld.py 在同一个文件夹中
sudo docker exec -i mycontainername python3 < a.py
It says它说
ModuleNotFoundError: No module named 'b' ModuleNotFoundError:没有名为“b”的模块
a.py:一个.py:
import b
print("Hello world")
I think that the module 'b' is searched in the directory /home inside the docker.我认为模块 'b' 是在 docker 内的目录 /home 中搜索的。 But putting the module inside the docker is not the solution I want, since I'm constantly editing that file但是将模块放在 docker 中并不是我想要的解决方案,因为我一直在编辑该文件
I'm constantly modifying a.py and b.py so I would want a command to run a.py with a command line like the following (I don't want to create the entire container over and over whenever I make a little change in the scripts):我一直在修改 a.py 和 b.py 所以我想要一个命令来运行 a.py 使用如下的命令行(我不想在我做一点改变的时候一遍又一遍地创建整个容器在脚本中):
sudo docker exec -i mycontainername python3 < a.py
Is there any command to do this?有什么命令可以做到这一点吗? Thanks in advance提前致谢
I agree with David that maybe you don't need to involve docker here, but if you really do then volumes will do what you need.我同意大卫的观点,也许你不需要在这里让 docker 参与进来,但如果你真的这样做了,那么卷将满足你的需要。 You can map a directory in your local machine to a directory in the running container.您可以将本地计算机中的目录映射到正在运行的容器中的目录。
For example, if I create a directory called src
and in it I create two files - a.py:例如,如果我创建一个名为src
的目录并在其中创建两个文件 - a.py:
import b
print("hello from a")
and b.py和 b.py
print("hello from b")
Now run a container from your image with local src
mapped to your container working directory:现在从您的映像运行一个容器,并将本地src
映射到您的容器工作目录:
docker run -it -d --name mycontainername -v $(pwd)/src:/home sample-image:latest
now you can run a.py
in the container:现在您可以在容器中运行a.py
:
docker exec -i mycontainername python3 a.py
you should see你应该看到
hello from b
hello from a
All good so far.到目前为止一切都很好。 Now on your local machine, edit src/b.py
or src/a.py
and try that docker exec
on the same running container, and the changes will have taken effect in the container.现在在您的本地机器上,编辑src/b.py
或src/a.py
并在同一个正在运行的容器上尝试 docker docker exec
,这些更改将在容器中生效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.