Home:ALL Converter>Create a file outside of Docker container while container is running in Python

Create a file outside of Docker container while container is running in Python

Ask Time:2023-01-07T23:11:12         Author:terry_develop12

Json Formatter

I currently have a Python application running in a Docker container on Ubuntu 20.04.

In this Python application I want to create a text file every few minutes for use in other applications on the Ubuntu server. However, I am finding it challenging to create a file and save it on the server from inside a containerised Python application.

The application Dockerfile/start.sh/main.py files reside in /var/www/my_app_name/ and I would like to have the output.txt file that main.py creates in that same folder, the location of the Dockerfile/main.py source.

The text file is created in Python using a simple line:

text_file = open("my_text_file.txt", "wt")

I have seen that the best way to do this is to use a volume. My current docker run which is called by batch script start.sh includes the line:

docker run -d --name=${app} -v $PWD:/app ${app}

However I am not having much luck and the file is not created in the working directory where main.py resides.

Author:terry_develop12,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/75041402/create-a-file-outside-of-docker-container-while-container-is-running-in-python
yy