简体   繁体   English

在Docker容器外运行python代码

[英]Running python code outside Docker container

I'm not able to run python code through my dockerized Meteor app. 我无法通过我的dockerized流星应用程序运行python代码。

I'm new to working with docker, and I have only just understood the principle of containers. 我是使用docker的新手,而我才刚刚了解容器的原理。 Using Meteor up ! 流星了 I have deployed a basic Meteor app to my server. 我已经在服务器上部署了基本的Meteor应用。

This app is deployed using Docker. 此应用是使用Docker部署的。 My goal of this app is to trigger python code. 这个程序的目标是触发python代码。 I can run the python code in the terminal through SSH, but when trying to run it from Meteor it can't find python3. 我可以通过SSH在终端中运行python代码,但是当尝试从Meteor运行它时,找不到python3。

What would be a good practice to run python code from inside the meteor app? 从流星应用程序内部运行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();
}

Looking at the Docker logs it now returns /bin/sh: 1: python3: not found Because python3 is installed correctly and working through ssh I assume it's running the code inside the meteor container. 查看Docker日志,它现在返回/ bin / sh:1:python3:找不到因为python3已正确安装并通过ssh运行,所以我认为它正在流星容器内运行代码。

UPDATE 1: I have tried adding Python to my container. 更新1:我尝试将Python添加到我的容器中。 I have added the following commands to the docker buildInstructions: 'RUN apt-get update && apt-get -y upgrade && apt-get install -y python3-pip && pip3 install setuptools' After this I tried to run my setup.py from within the project because I couldn't find access to my python project files from within the script. 我在docker buildInstructions中添加了以下命令:'RUN apt-get update && apt-get -y upgrade && apt-get install -y python3-pip && pip3 install setuptools'之后,我尝试从以下位置运行setup.py在项目中进行访问,因为我无法从脚本中访问我的python项目文件。

I am currently looking for a way to run my setup.py file from within the meteor project without any success. 我目前正在寻找一种从流星项目中运行setup.py文件但没有成功的方法。 Any thoughts on how to proceed? 对如何进行有任何想法吗?

My final solution that fixed my problems was the following setup: 解决我的问题的最终解决方案是以下设置:

Python docker-compose with Flask to make the code accessible. Python与Flask docker-compose一起使代码可访问。 Using two volumes for both the projects that pointed to the same folder to share the data I needed to perform calculations in Python. 为两个指向同一文件夹的项目使用两个卷以共享我在Python中执行计算所需的数据。

After the calculations I returned the results in JSON format to the Meteor app so the results could be imported into the Mongo database. 计算之后,我将结果以JSON格式返回到Meteor应用程序,以便可以将结果导入到Mongo数据库中。

I hope this helps anyone who is facing the same problem! 希望这对遇到同样问题的任何人有所帮助! And thanks for everyone who helped figuring out a way on how to solve this issue. 感谢所有为解决此问题的方法提供帮助的人。

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

相关问题 当容器在 Python 中运行时,在 Docker 容器之外创建一个文件 - Create a file outside of Docker container while container is running in Python 在run -it / bin / bash之外的docker容器中发出运行python脚本的问题 - Issue running python scripts in docker container outside of run -it /bin/bash 从 Docker 容器中运行的 Python 代码访问 Windows CIFS 共享 - Accessing a Windows CIFS share from Python code running in a Docker container 使用 vscode 从主机调试 docker 容器中运行的 python 代码 - debug python code running in docker container from host using vscode Python 模块未在 docker 容器中运行 - Python module not running in docker container 在 docker 容器之外运行 pytest 命令失败,因为容器已停止 - Running pytest command outside of docker container failes because container stopped 通过 python 在 docker 容器外写入文件 - Write files outside of a docker container via python 在 Docker 容器外运行 python 文件,该文件在 Docker 容器外导入 python 文件 - Run python file outside Docker container that imports a python file outside Docker container 如何通过运行带有URL的docker容器来连接docker容器中的python应用 - how to connect python app in docker container with running docker container with url Docker 容器卡在从 Python SDK 运行 - Docker container stuck running from Python SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM