Timo Frionnet
Timo Frionnet

Reputation: 484

Running python code outside Docker container

I'm not able to run python code through my dockerized Meteor app.

I'm new to working with docker, and I have only just understood the principle of containers. Using Meteor up! I have deployed a basic Meteor app to my server.

This app is deployed using Docker. My goal of this app is to trigger python code. I can run the python code in the terminal through SSH, but when trying to run it from Meteor it can't find python3.

What would be a good practice to run python code from inside the meteor app?

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.

UPDATE 1: I have tried adding Python to my container. 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.

I am currently looking for a way to run my setup.py file from within the meteor project without any success. Any thoughts on how to proceed?

Upvotes: 0

Views: 1795

Answers (1)

Timo Frionnet
Timo Frionnet

Reputation: 484

My final solution that fixed my problems was the following setup:

Python docker-compose with Flask to make the code accessible. Using two volumes for both the projects that pointed to the same folder to share the data I needed to perform calculations in 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.

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.

Upvotes: 1

Related Questions