copy file from docker to host system using python script - python

I have logged into the docker from the below command, now from the python script i want to copy the file from docker to host system how to do this
sudo docker run -ti video:new /bin/bash
import os
os.system('cp /tmp/a.txt HOST:/tmp/a.txt')

Map a volume to share data with your host from the container.
docker run -v /tmp/:/tmp/ -ti video:new /bin/bash
Then let your python script copy the file to the /tmp directory inside your container.
import os
os.system('cp /path/to/a.txt /tmp/a.txt')
Through to the -v mapping, the file is placed on the docker host in the directory /tmp. Once you close your docker container, the file will still exist on the host as /tmp/a.txt.

The container can't copy information outside its isolation. If you wanna share information between container and host, please use volume mapper to do that (-v):
https://docs.docker.com/userguide/dockervolumes/

Related

Save the output dataframe from docker to a local machine

Tried with {WORKDIR} & replaced {ADD} with {COPY} in Dockerfile. Still the CSV file is not being saved in the local folded in the local machine, why is this?
In order to write to a host file from the Docker container, first volume mount a directory into the running container.
For example, we can mount the current working directory into a directory in the container:
$ docker run -v $(PWD):/pwd_in_container -it --rm busybox touch /pwd_in_container/test
Then the file we created in the container will be available on the host:
$ ls test
test

Attempting to mound my directory in docker (to run pyspark)

I have a directory which I am trying to mount in docker to use a pyspark container.
I am passing the following code to try and host my local directory in the container
docker run -it -p 8888:8888 -v {absolute file path}:/home/jovyan/work --rm jupyter/pyspark-notebook
Users/{myname}/Documents/{filepath}" 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.
Any ideas on whats going wrong and next steps would be greatly appreciated

Docker Run: Mounted Volume not showing change in files

I am struggling with running the latest changes. Below are the details.
Dockerfile
FROM python:3.7.3
RUN mkdir -p /usr/apps
COPY test.py /usr/apps
RUN pip install mindsdb
CMD [ "python","test.py" ]
Build
docker build -t py37:custom .
Run
docker run -it -v /Development/PetProjects/mindsdb:/usr/apps/ py37:custom
But it shows only the changes at the time of build.
First of all while starting your container you are not using volumes but bind mounts. So you mount directory /Development/PetProjects/mindsdb on your host machine to /usr/apps/ directory. Every change made to files on your host machine in this directory, will be visible in the container, and the other way round.
If you wanted to use volumes, you could create one using docker volume create command and then running container with this volume : docker container run -v volume_name:path_in_container image_name. Then you would be able to stop container and run it again by passing this volume to run command and changes to path_in_container directory could be stored across container creations.
Another thing is that you are trying to mount /usr/apps/ in your container and you copied a python script there using Dockerfile. Note that in you current docker run command contents of /Development/PetProjects/mindsdb will replace content of /usr/apps/ in your container and if you do not have your script in /Development/PetProjects/mindsdb - script will not be visible in the container.
Moreover your CMD seems not to work because of path relativeness. You should change your CMD to CMD [ "python","/usr/apps/test.py" ] or use WORKDIR option - WORKDIR /usr/apps/ so your python command could be executed from this directory and script could be visible there.
More information about differences between volumes and bind mounts can be found in docker documentation.

Run Python from Docker

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"

how to pass argument (file path) to python app in Docker

I have a python application that is expecting first argument as file path. basically a configuration file.
This file it should get form volume/ mount in Docker
How to pass this:
. Snippets
Python:
with open(sys.argv[1], 'r') as ymlfile:
cfg = yaml.load(ymlfile)
Docker file
COPY install.py /wiki/install.py
CMD [ "python", "/wiki/install.py", "/config/config.yml"]
Run image command
sudo docker run -v /config:/home/example/config/ app-wiki
I am expecting config.yml file available at /home/example/config/ will be copied in /config dir and inside Docker file
it will be available
but its not working this way.
Where I am going wrong?
The way docker CMD works is you need to make sure that the file is copied into the correct location with correct permissions.
Example:
RUN mkdir -p /config
RUN mkdir -p /wiki
ADD cp <your-location> /config/config.yml
ADD install.py /wiki/install.py
CMD [ "python", "/wiki/install.py", "/config/config.yml"]
Also, docker will keep the same permissions that you have in your local directory. So, make sure you have correct permission set on both files.
The issue is that you have got the direction wrong. The format is <hostpath>:<containerpath>
Below
sudo docker run -v /config:/home/example/config/ app-wiki
should be
sudo docker run -v /home/example/config/:/config app-wiki
Doing the config present in /home/example/config/ will be available in container at /config folder
Edit-1
Added a bit of more explanation to clear you doubts.
COPY install.py /wiki/install.py
CMD [ "python", "/wiki/install.py", "/config/config.yml"]
When you run the above image it will expect a config to be available at /config/config.yml.
Now if you have a folder on your host /home/tarun/wikiconfig which has a config.yml file then you run the container using
sudo docker run -v /home/tarun/wikiconfig:/config app-wiki
If the name of the config.yml file is different in your wikiconfig folder, then you will mount the file to config.yml
sudo docker run -v /home/tarun/wikiconfig/myconfig.yml:/config/config.yml app-wiki
Both would override the config you added when you build the Dockerfile, because when you a mount a folder or file from host, anything with the same path inside container is no more accessible to the image

Categories