简体   繁体   English

将参数传递给在Docker容器中运行的python脚本

[英]Pass argument to python script running in a docker container

Suppose the following setup: 假设以下设置:

  • Website written in php / laravel 用php / laravel编写的网站
  • User uploads a file (either text / doc / pdf) 用户上传文件(文本/ doc / pdf)
  • We have a docker container which contains a python script for converting text into a numpy array. 我们有一个docker容器,其中包含一个python脚本,用于将文本转换为numpy数组。

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: 顺便说一下,在这种情况下,您将从以下方面受益:

  • You can detect whether your python service is online 您可以检测您的python服务是否在线
  • You can move python container to another machine 您可以将python容器移动到另一台机器

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: 有两个主要警告:

  1. 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服务也在容器中运行,则需要知道一些您可以写入的主机系统路径,该路径在容器文件系统中也是可见的。

  2. 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.

相关问题 如何将参数传递给在 docker 容器内运行的 python 脚本? - How to pass arguments to the python script that is running inside docker container? 在 nginx docker 容器内运行 python 脚本 - Running a python script inside an nginx docker container 运行 docker-compose 时如何将参数传递给 docker 容器 - How to pass argument to docker container when running docker-compose up 如何将 Bash 命令从 Python 传递到正在运行的 Docker 容器 - How to pass Bash commands from Python to a running Docker Container 将参数传递给在AWS Fargate上的Docker容器中运行的Python - Pass arguments to Python running in Docker container on AWS Fargate 如何将参数传递给具有 python 脚本的 docker 图像 - How to pass argument to docker image having python script 无法将引用的字符串传递给docker容器中的python脚本 - Unable to pass quoted strings to the python script within a docker container 运行docker容器以最小的占用空间执行python脚本 - Running a docker container to execute python script with minimal footprint 如何在docker容器中导入python模块运行脚本? - How to import python module running script in docker container? 在 Docker 容器错误的时区内运行 python 脚本 - Running python script inside Docker container wrong timezone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM