How to execute a local python script into a docker from another python script?

Viewed 29041

Let me clarify what I want to do.

I have a python script in my local machine that performs a lot of stuff and in certain point it have to call another python script that must be executed into a docker container. Such script have some input arguments and it returns some results.

So i want to figure out how to do that.

Example:

def function()
    do stuff
         .
         .
         .
    do more stuff

    ''' call another local script that must be executed into a docker'''

    result = execute_python_script_into_a_docker(python script arguments)

The docker has been launched in a terminal as:

docker run -it -p 8888:8888 my_docker
3 Answers

You can use docker's python SDK library. First you need to move your script there, I recommend you do it when you create the container or when you start it as Callmemath mentioned:

docker run -it -v myFile.py:/myFile.py -p 8888:8888 my_docker

Then to run the script using the library:

...
client = docker.client.from_env()
container = client.containers.get(CONTAINER_ID)
exit_code, output = container.exec_run("python your_script.py script_args")
... 
Related