简体   繁体   English

运行docker容器以最小的占用空间执行python脚本

[英]Running a docker container to execute python script with minimal footprint

I have a node.js server that gets an python script as an input from the web client (Yes, risky), the server runs the script and return the output to the client. 我有一个node.js服务器,它从Web客户端获取python脚本作为输入(是的,有风险),该服务器运行该脚本并将输出返回给客户端。

In order to minimize the risks here, I'm creating a docker container for the execution of the script with the following command: 为了最大程度地减少此处的风险,我使用以下命令创建了一个Docker容器来执行脚本:

docker run -it --rm --name python-script-instance -v /dir/on/server:/tmp python:2 python /tmp/script.py

It requires creating a directory on the server that will hold a file named script.py , the host server directory is mounted to /tmp on the container and allow access to script.py through /tmp/script.py 它需要在服务器上创建一个目录,该目录将保存一个名为script.py的文件,该主机服务器目录已安装到容器上的/tmp并允许通过/tmp/script.py访问script.py

I dont wan't to have script.py created on the server, I want to dynamically take it from memory directly into the docker container. 我不想在服务器上创建script.py,我想将其动态地从内存中直接带到docker容器中。

I thought about passing the entire script as a command line argument to the python interpreter, but was unsuccessful to get it to work. 我曾考虑过将整个脚本作为命令行参数传递给python解释器,但未能使其正常工作。

Any suggestions on how can I execute a python script encapsulated in a docker container leaving a minimal footprint on the host server? 关于如何执行封装在Docker容器中的python脚本的任何建议,在主机服务器上的占用空间最小?

您可以使用Python解释器的-c选项内联提交脚本:

docker run --rm python:2 python -c 'print "Hello Python"'

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM