Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 314
Abiud Rds Avatar asked Jul 26 '17 10:07

Abiud Rds


People also ask

Can you call a Python script from another Python script?

There are multiple ways to make one Python file run another. 2. You can use the exec command. executes the file.py file in the interpreter.

How do I Dockerize a Python program?

Form your new directory by creating a new root project folder in the sidebar, and naming it. Open a new workspace named main.py . Enter the cd [root folder name] command in the Terminal to tap into your new directory. Copy and paste any pre-existing Python application code into your main.py workspace.


1 Answers

You can add your file inside docker container thanks to -v option.

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

And execute your python inside your docker with : py /myFile.py

or with the host:

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

And even if your docker is already running

docker exec -ti docker_name py /myFile.py

docker_name is available after a docker ps command.

Or you can specify name in the run command like:

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

It's like:

-v absoluteHostPath:absoluteRemotePath

You can specify folder too in the same way:

-v myFolder:/customPath/myFolder

More details at docker documentation.

like image 171
callmemath Avatar answered Sep 27 '22 00:09

callmemath