I'm writing a web application in Ruby on Rails where users can write python code in a web editor and execute it in an docker environment on the server. I've written a simple python code that creates a docker container:
import docker
import sys
if __name__ == "__main__":
if(len(sys.argv) == 2):
token = sys.argv[1]
client = docker.from_env()
res = client.containers.run('openql','python3 /home/pythonwrapper.py '+token)
else:
print("Requires one parameter")
As you can see it creates a docker container using the image openql and execute a simple python script inside. If a user press the execute button in the web editor Ruby on Rails executes this script by using the this command: system("python","script.py","<TOKEN>")
So far so good, this works all fine.
However, there is a problem executing the pythonwrapper.py
inside the docker container. I'm using python's requests library for requesting the files written by the user to execute them inside the docker container. The code looks like this:
# Request all the available assets, it does not download the content of the files.
# Downloading the content of the files is done in a later request
url = rails_url+"allAssets/"+token
res = requests.get(url)
#Convert bytes into string
content = str(res.content, 'utf8')
Looks pretty simple, but the whole ruby on rails webserver hangs during this request. And the strange thing is that it all works fine if I first executes this script manually from the console after restarting the server.
The only thing I get from the Rails console if this:
Started GET "/allAssets/123" for 10.0.2.15 at 2017-08-02 10:24:59 +0200
When I quit the webserver for a restart, Ruby on Rails shows the following logs:
And then nothing. Does anyone know what could be the problem?