3

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?

6
  • 4
    Mount an outside path to an inside path when starting the container, then write to the inside path from inside the container. Commented Feb 24, 2021 at 7:11
  • Im pretty new to containers.. not really sure what that means... could you elaborate a bit more or send some docs regarding this? thanks! Commented Feb 24, 2021 at 7:12
  • 3
    docs.docker.com/storage/volumes Commented Feb 24, 2021 at 7:13
  • Much appreciation! Commented Feb 24, 2021 at 7:16
  • 1
    If the goal of your application is to read and write host files, and you're running on a MacOS or Linux system that has Python preinstalled anyways, I wouldn't bring Docker into it; just directly run the Python script. Commented Feb 24, 2021 at 11:57

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

that is good, but what if I am hosting the container in Azure. How can I control the volume to mount?
Unfortunately I have no experience with Azure but you might want to consult their documentation. Maybe this helps: learn.microsoft.com/en-us/azure/container-instances/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.