Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write files outside of a docker container via python

Is there a way to write files (json in this case) to outside of the docker container? I'm taking about the most simple way, for example:

data = {"id":"1","name":"sample"}
with open('name.json','w') as fp:
   json.dump(data,fp,indent=4)

is there a way to make this write outside of the docker container? or in a specific place? or to save it on a specific path on the system that the container will run on?

like image 647
Slava Bugz Avatar asked Mar 04 '26 16:03

Slava Bugz


1 Answers

Let's assume

  • your python script has the location /app/myscript.py in your docker container.
  • the location for where your python script should write the files is /home/jon/result_files/
  • your docker image is called mypyimg

Then you can use volumes to mount a host location when starting your container:

docker run -d \
  --name my_py_container \
  -v /home/jon/result_files:/app \
  mypyimg
like image 98
commonist Avatar answered Mar 06 '26 06:03

commonist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!