在Docker容器外运行python代码

时间:2019-01-18 16:52:16

标签: python docker meteor

我无法通过我的dockerized流星应用程序运行python代码。

我刚接触docker,但我才刚刚了解容器的原理。使用Meteor up!我已经在服务器上部署了基本的Meteor应用。

此应用是使用Docker部署的。这个程序的目标是触发python代码。我可以通过SSH在终端中运行python代码,但是当尝试从Meteor运行它时,找不到python3。

从流星应用程序内部运行python代码是什么好习惯?

    const Future = Npm.require("fibers/future");
    // Load exec
    const exec = Npm.require("child_process").exec;
    // This method call won't return immediately, it will wait for the
    // asynchronous code to finish, so we call unblock to allow this client
    // to queue other method calls (see Meteor docs)
    console.log('before unblock');
    this.unblock();
    console.log('starting futures');
    const future = new Future();
    const command = `python3 ~/python_project/run.py '${fileName}' '${name}'`;
    console.log('before execution');
    exec(command,function(error,stdout,stderr) {
        console.log('during execution');
        if (error) {
            console.log(error);
            throw new Meteor.Error(500,command+" failed");
        }
        future.return(stdout.toString());
    });
    console.log('after execution');
    return future.wait();
}

现在查看Docker日志会返回/ bin / sh:1:python3:找不到 因为python3已正确安装并通过ssh进行工作,所以我认为它正在流星容器中运行代码。

更新1: 我尝试将Python添加到我的容器中。我已经在docker buildInstructions中添加了以下命令:'RUN apt-get update && apt-get -y upgrade && apt-get install -y python3-pip && pip3 install setuptools' 之后,我尝试从项目中运行setup.py,因为我无法从脚本中访问我的python项目文件。

我目前正在寻找一种从流星项目中运行我的setup.py文件的方法,但没有成功。对如何进行有任何想法吗?

1 个答案:

答案 0 :(得分:1)

解决我的问题的最终解决方案是以下设置:

Python docker与Flask组合以使代码可访问。 为两个指向同一文件夹的项目使用两个卷以共享我在Python中执行计算所需的数据。

计算之后,我将结果以JSON格式返回到Meteor应用程序,以便将结果导入到Mongo数据库中。

我希望这对遇到相同问题的所有人有所帮助! 感谢所有为解决此问题的方法提供帮助的人。