簡體   English   中英

在Docker容器外運行python代碼

[英]Running python code outside Docker container

我無法通過我的dockerized流星應用程序運行python代碼。

我是使用docker的新手,而我才剛剛了解容器的原理。 流星了 我已經在服務器上部署了基本的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文件但沒有成功的方法。 對如何進行有任何想法嗎?

解決我的問題的最終解決方案是以下設置:

Python與Flask docker-compose一起使代碼可訪問。 為兩個指向同一文件夾的項目使用兩個卷以共享我在Python中執行計算所需的數據。

計算之后,我將結果以JSON格式返回到Meteor應用程序,以便可以將結果導入到Mongo數據庫中。

希望這對遇到同樣問題的任何人有所幫助! 感謝所有為解決此問題的方法提供幫助的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM