[英]Pass argument to python script running in a docker container
Suppose the following setup: 假设以下设置:
I want to take this uploaded data and pass it to the python script. 我想获取此上传的数据并将其传递给python脚本。
I can't find anything which explains how to pass dynamically generated inputs into a container. 我找不到任何解释如何将动态生成的输入传递到容器中的信息。
Can this be done by executing a shell script from inside the laravel app which contains the uploaded file as a variable specified in the dockerfile's ENTRYPOINT? 是否可以通过从laravel应用程序内部执行shell脚本来完成此任务,该脚本包含上传的文件作为dockerfile的ENTRYPOINT中指定的变量?
Are there any other ways of doing this? 还有其他方法吗?
One way to do this would be to upload the files to a directory to which the Docker container has access to and then poll the directory for new files using the Python script. 一种方法是将文件上传到Docker容器可以访问的目录,然后使用Python脚本在目录中轮询新文件。 You can access local directories from Docker containers using "bind mounts" . 您可以使用“ bind mounts”从Docker容器访问本地目录。 Google something like "How to share data between a Docker container and host system" to read more about bind mount and sharing volumes. Google类似“如何在Docker容器和主机系统之间共享数据”之类的内容,以了解有关绑定安装和共享卷的更多信息。
I would strongly recommend using tcp/ip for such purposes. 我强烈建议将tcp / ip用于此类目的。 By the way, in this case you benefit from: 顺便说一下,在这种情况下,您将从以下方面受益:
Implementation is really simple. 实现真的很简单。 You can choose any framework, but for me suitable is Twisted
, and implement your python script as follows: 您可以选择任何框架,但对我来说合适的是Twisted
,并按如下所示实现您的python脚本:
from twisted.internet.protocol import Factory, Protocol
from twisted.protocols.basic import LineReceiver
class DataProcessor(LineReceiver):
def lineReceived(self, line):
# line contains your data
pass
Factory factory = Factory()
factory.protocol = DataProcessor
reactor.listenTCP(8080, factory)
... a python script for ... ...用于...的python脚本
Just run it; 只是运行它; don't package it into a Docker container. 不要将其打包到Docker容器中。 That's doubly true if its inputs and outputs are both local files, and it expects to do its thing and exit promptly: the filesystem isolation Docker provides works against you here. 如果它的输入和输出都是本地文件,并且它希望执行该操作并立即退出,则这是双重事实:Docker提供的文件系统隔离在这里不利于您。
This is, of course, technically possible. 当然,这在技术上是可能的。 Depending on how exactly the support program container is set up, the "command" at the end of docker run
will be visible to the Python script in sys.argv
, like any other command-line options. 取决于支持程序容器的设置方式,与其他命令行选项一样, docker run
结束时的“命令”对于sys.argv
的Python脚本可见。 You can use a docker run -v
option to publish parts of the host's filesystem into the container. 您可以使用docker run -v
选项将主机文件系统的一部分发布到容器中。 So you might be able to run something like 因此,您可能可以运行类似
docker run --rm -v $PWD/files:/data \
converter_image \
python convert.py /data/in.txt /data/out.pkl
where all of the /data
paths are in the container's private filesystem space. 所有/data
路径都在容器的专用文件系统空间中。
There are two big caveats: 有两个主要警告:
The host paths in the docker run -v
option are paths specifically on the physical host . docker run -v
选项中的主机路径是物理主机上的专用路径。 If your HTTP service is also running in a container you need to know some host-system path you can write to that's also visible in your container filesystem. 如果您的HTTP服务也在容器中运行,则需要知道一些您可以写入的主机系统路径,该路径在容器文件系统中也是可见的。
Running any docker
command at all effectively requires root privileges. 完全有效地运行任何docker
命令都需要root权限。 If any of the filenames or paths involved are dynamic, shell injection attacks can compromise your system . 如果涉及的任何文件名或路径都是动态的,则外壳注入攻击可能会危害您的系统 。 Be very careful with how you run this from a network-accessible script. 要特别注意如何从可通过网络访问的脚本运行此脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.