Run python script from Docker - python

Trying to run test Python script from Docker using Bash. Win10 OS.
I have a file print.py in the directory C:/Py/test
run this $ cd C:/Py/test
run this $ docker run --volume $(pwd):/home/${USER} python:3.7 python /home/${USER}/print.py
Got this error: python: can't open file 'C:/Program Files/Git/home/print.py': [Errno 2] No such file or directory
Why is this file trying to be found in the C:/Program Files/Git/home/ directory instead of C:\Py\test?
$ python 'print.py' is working

Git bash prepended its installation path, try these two methods :
Use: //home/${USER}/print.py
Run with: MSYS_NO_PATHCONV=1 docker run ...

Related

Running bash script with python on local machine

I'm trying to run a bash script on my python script with
subprocess and also tried with os.system but failed to do so
the command that I tried to execute is:
totp <<(echo "<some_code>")
I created 'totp' bash file with the following commands:
cd /usr/local/bin
touch totp
vim totp
copy and paste the bash script content
and then
chmod +x totp
Via terminal of the PyCharm I run the bash command fine but via python code, I get the error:
FileNotFoundError: [Errno 2] No such file or directory: 'totp <<(echo "")'

Locate files written by a python script in a container that is not running anymore

I have a python script that is dumping a pandas dataframe into a file using :
dataframe.to_csv(file.csv)
After executing the docker container using:
sudo docker-compose up freqtrade # ...rest of the command
I can't locate the file.csv and my cwd is freqtrade/ but can't access the filesystem in the container.
Also tried to save the file to a path in the ubuntu file system but got the following error:
FileNotFoundError: [Errno 2] No such file or directory: '/home/kato/freqtrade/'
If the container has been stopped and has not been removed you should still find it using docker ps with -a:
docker ps -a
If you found the container ID, you can then copy files from its filesystem without starting it using docker cp:
docker cp CONTAINER_ID:absolute/path/to/file ./host/target/path
Another solution to avoid doing this in the future, you could run your container with a bind mount (a directory from your machine would be mapped to a directory inside the container):
docker run -v "$(pwd)"/relative/host_dir:/absolute/container_dir my_image

python: can't open file 'manage.py': [Errno 2] No such file or directory docker-compose run

I tried run django project with code
docker-compose -f local.yml run --rm django python manage.py runserver
but i had such problem:
python: can't open file 'manage.py': [Errno 2] No such file or directory
in another computer this project run
but this code work
docker-compose -f local.yml run django python
I get that error when I am working in the wrong directory as the manage.py. Double check which directory that you are in.

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

Run a Python script in Vagrant

I'm learning how to use Vagrant with a Udacity course, and we're asked to run a Python script database_setup.py in our virtual machine.
For this, I created a folder "udacityproject" inside my vagrant folder on my computer. I saved my file database_setup.py there.
Now on Bash, I do
$ vagrant up
$ vagrant ssh
$ cd udacityproject
$ python database_setup.py
The interface returns:
"python: can't open file 'database_setup.py': [Errno 2] No such file
or directory".
It must be a silly mistake, but I cannot see what I am doing wrong... A similar topic was opened here (Run Python script in Vagrant) but the answers are not helping me.
The vagrant folder on your computer, which contains the file VagrantFile, is the folder /vagrant on your vm (It is under /). It's not your home directory. After vagrant ssh you are logged in home directory of user vagrant. It's /home/vagrant/.
$ vagrant ssh
$ pwd
/home/vagrant
The tree looks like that:
/root
/tmp
/usr
/var
/home
|-- vagrant # <-- You are here after logging
/vagrant
|-- udacityproject
|-- database_setup.py # <-- Your script is here
...
To run your script you must go to /vagrant
$ cd /vagrant
With ls * you can check if your file exists. Now go to your created folder and run your script
$ cd udacityproject
$ python database_setup.py
Or simply do that from beginning:
$ vagrant ssh
$ python /vagrant/udacityproject/database_setup.py

Categories