Run Python from Docker - python

I'm trying out Docker these days and I want to run create virtual environments in Python in Docker. I downloaded Miniconda3 from docker hub and tested out with basic hello world program written in python.
I ran:
docker run -i-t continuumio/miniconda3 /bin/bash
Then on another terminal I ran:
docker exec laughing_wing "python ~/Documents/Test/hello_world.py"
Where the name of docker container is laughing_wing, and my hello_world.py is in Documents/Test directory.
But running the second command I get:
"OCI runtime exec failed: exec failed: container_linux.go:344:
starting container process caused "exec: \"python
~/Documents/Test/hello_world.py\": stat python
~/Documents/Test/hello_world.py: no such file or directory": unknown"
I'm confused about this.

Looks like you're trying to have the docker container run a python file from your machine. The docker container is isolated from it's host, so you need to either create your own Docker image where you add the file, or mount the ~/Documents/Test directory to your docker container. Something like this:
docker run -it -v ~/Documents/Test:/Test continuumio/miniconda3 /bin/bash
docker exec *container_name* "python /Test/hello_world.py"

Related

docker: Error response from daemon: create ${PWD}: "${PWD}" includes invalid characters for a local volume name,

I am getting the full error
docker: Error response from daemon: create ${PWD}: "${PWD}" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.
When I try running the command
docker run --gpus all --rm -it -v ${PWD}:/home/EEGNet --name EEGNet_inner_speech eegnet_env:latest python experiment.py
I am on Windows 10, 21H2, -19044.1706, though it might have to do with the Intel(R) Core(TM) i5-6300U CPU #2.40GHz.
This question was previously asked here however if I switch my file path for the ${PWD} such as
docker run --gpus all --rm -it -v C:\Users\wesie\EEGNet_inner_speech --name EEGNet_inner_speech eegnet_env:latest python experiment.py
docker run --gpus all --rm -it -v C:\Users\wesie\EEGNet_inner_speech eegnet_env:latest python experiment.py
or switch ${PWD} to %cd% as the previous question suggested I get the error
docker: Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: Running hook #0:: error running hook: signal: segmentation fault, stdout: , stderr:: unknown.
Second error asked here previously in stack overflow. and here in Docker's Community
Any idea on how to fix these? Full directions:
Run git clone https://github.com/naomike/EEGNet_inner_speech.git
Place dataset under dataset directory Build a docker container by
running docker image build -t eegnet_env:latest .
Run docker run --gpus all --rm -it -v ${PWD}:/home/EEGNet --name EEGNet_inner_speech eegnet_env:latest python experiment.py, and you
see resulting files created in the directory.

Docker Python ImportError when started inline with container

I am having an odd problem when running a python script in a docker container.
When I start the script in the same line as I am starting the docker containter e.g.
docker run -it --rm <containter>:<version> /bin/bash --login -c "python /opt/project/main.py"
It raises an ImportError for a module.
However when I first start the docker conainer and afterwards start the script afterwards
docker run -it --rm <containter>:<version> /bin/bash
python /opt/project/main.py
everything behaves as it should. So only when i start the script in the same line, the issue occurs.
Hope you can give me a heads-up. Thanks!
I did find a solution which I am happy to share with a random googlers:
The issue I had was that the python dependencies I used were source build catkin dependencies. Thus the setup.bash file from the catkin workspace needed to be sourced in order for the libraries to be found. Since the .bashrc is not sourced when starting the docker like I mentioned, it as to be done manually:
docker run -it --rm <containter>:<version> /bin/bash --login -c "source /path/to/setup.bash && python /opt/project/main.py"

How to run a simple Python script without writing a complete Dockerfile?

I have set up Docker Toolbox on a Win 10 machine. I have some simple single file Python scripts that I want to run in Docker, just for learning purpose.
Started learning Docker today, and Python 3 days ago.
I assume I have set up Docker correctly, I can run the example hello-world image. No error messages during setup.
I am following an instruction from here https://runnable.com/docker/python/dockerize-your-python-application,
which says:
If you only need to run a simple script (with a single file), you can avoid writing a complete Dockerfile. In the examples below, assume you store my_script.py in /usr/src/widget_app/, and you want to name the container my-first-python-script:
docker run -it --rm --name my-first-python-script -v "$PWD":/usr/src/widget_app python:3 python my_script.py
If I type pwd, it shows:
/c/Program Files/Docker Toolbox
And the script I want to run is located here:
C:\Docker\Python\my_script.py
This is what I think should work:
docker run -it --rm --name my-first-python-script -v "$PWD":/c/Docker/Python python:3 python my_script.py
No matter how I try to specify the file directory, I get an error:
python: can't open file 'my_script.py': [Errno 2] No such file or directory
When you run -v "$PWD":/c/Docker/Python, you are saying you want to link your current working directory to the path /c/Docker/Python in the container, which isn't what you want to do. What you are trying to do is link C:\Docker\Python\ on your host to the container folder /usr/src/widget_app.
This command will put your script inside the container path /usr/src/widget_app, then run it:
docker run -it --rm --name my-first-python-script -v /c/Docker/Python:/usr/src/widget_app python:3 python /usr/src/widget_app/my_script.py

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
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.
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")
...
you have to use docker exec -it image_name python /filename
Note: To use 'docker exec' you must run the container using docker run

Using iPython with remote docker container in a local project directory

I am completely new to Docker and I followed the instructions found on this page https://cmusatyalab.github.io/openface/setup/:
docker pull bamos/openface
docker run -p 9000:9000 -p 8000:8000 -t -i bamos/openface /bin/bash
cd /root/openface
./demos/compare.py images/examples/{lennon*,clapton*}
And I was able to run an example of openface following the example. However, normally I develop in iPython and would like to do so. However, I cannot import openface from iPython, since presumably it is not installed locally. Similarly, I do not know how to cd into my project directory, which is in /Users/name/documents/my-project.
What is the idiomatic way to proceed?
My recommended approach is using docker volumes. As you already have the project outside the container, you can start the container with a volume to map your project directory to a directory inside the container. That's idiomatic.
For instance:
docker run -v /Users/name/Documents/my-project:/root/my-project -p 9000:9000 -p 8000:8000 -t -i bamos/openface /bin/bash

Categories