Not able to set/override docker entrypoint to execute two commands - python

I am trying to set/override the docker entrypoint when I am launching my docker image but I am getting an unexpected behavior
Scenario 1 (Not working)
Docker Run Command --> docker run --rm -it --privileged --net=host
python3.6 -m CameraServerBasler.pylon_video &
python3.6 -m CameraServerBasler.server
Output --> /usr/bin/python3.6: Error while finding module
specification for 'CameraServerBasler.server' (ModuleNotFoundError:
No module named 'CameraServerBasler')
Scenario 2 (Working)
If I execute the same command inside the docker image bash everything is working as expected
Docker Run Command --> docker run --rm -it --privileged --net=host --entrypoint=/bin/bash
Command executed inside the docker image --> python3.6 -m
CameraServerBasler.server & python3.6 -m
CameraServerBasler.pylon_video
Output --> both servers(modules) up and running as expected (server
and pylon_video)
Docker image --> ubuntu:18.04
Am I missing something in the docker run command?

I have been able to resolve this issue using a sh script, but still not very clear why is not working using the Approach #1
Approach #1 Not Working
Entry point declared in the docker file
ENTRYPOINT ["python3.6", "-m", "CameraServerBasler.server", "&", "python3.6", "-m", "CameraServerBasler.pylon_video"]
Approach #2 Working
Entry point declared in the docker file
ENTRYPOINT ["sh", "init.sh"]
init.sh File Content
python3.6 -m CameraServerBasler.server & python3.6 -m CameraServerBasler.pylon_video

Related

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

bash: ./run: No such file or directory

Im new to docker.
I am starting the run command with a script called r, which has the following code
proxy="--build-arg http_proxy=http://wwwcache.open.ac.uk:80 --build-arg https_proxy=http://wwwcache.open.ac.uk:80"
if [ "$http_proxy" == "" ]; then
proxy=
fi
docker build $proxy -t bi-tbcnn docker
docker run -v $(pwd):/e -w /e --entrypoint bash --rm -it bi-tbcnn -c ./run
When I execute r I am getting the following error
bash: ./run: No such file or directory
but when I directly execute the ./run command on my terminal is ok
I use Docker Toolbox on windows
The project address is https://github.com/bdqnghi/bi-tbcnn
thanks
This is a known issue on docker for windows
https://blogs.msdn.microsoft.com/stevelasker/2016/09/22/running-scripts-in-a-docker-container-from-windows-cr-or-crlf/
it seems you're facing an issue with Carriage Return(CR) and Line Feeds(LF) characters, maybe your code editor is changing the newline format automatically
can you to try open a bash session on the container and execute the script manually?
docker run -v $(pwd):/e -w /e --entrypoint bash --rm -it bi-tbcnn
root#a83fcd779f8e:/e# ./run
Please paste the output here

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