Reputation: 505
I'm a trying to dockerize a python script in which file writing of a text file and CSV file is there. but it is not working, the file is not getting generated.
For reference my code is
FROM python:3
ADD ./HMS.py /
ADD ./constants.py /
ADD ./Docker.py /
ADD ./SNT.py /
ADD ./Utility.py /
RUN pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org docker
RUN pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org grepfunc
RUN pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org requests
CMD [ "python", "./HMS.py" ]
However, when I run the python script outside am able to generate the files. May I know why it is not getting generated inside the work directory.
def write_log(self, filename, message, type='info'):
with open(filename, 'a') as log:
log.write('\n' + message)
def write_csv(self, filepath, data):
with open(filepath, 'a', newline='') as file:
writer = csv.writer(file)
writer.writerow(data)
constant file contain the path
LOG_FILE='./snt_alarm_log.txt'
COMPONENT_NAME='SNT Alarm'
DB_FILE='./snt.csv'
Upvotes: 5
Views: 3390
Reputation: 21034
I am on a Windows machine with a Git bash trying to follow this tutorial. When I run this command to execute a Python script to write a file, I don't see the file in my local directory!
$ docker run --volume="/c/Users/nanders2/.python-xml-docker":/app pythonxml:latest
The issue is with Git Bash's Posix path conversion as described here.
This is due to Mingw/msys2 doing automatic path conversion; see http://www.mingw.org/wiki/Posix_path_conversion
You can disable this by prefixing paths with // or setting the MSYS_NO_PATHCONV=1 environment variable (see https://stackoverflow.com/a/34386471)
Because this path conversion happens before docker receives the value, its not something we can fix in docker.
The solution that works for me is to use double-forward-slash in my path
$ docker run --volume="//c//Users//nanders2//.python-xml-docker":/app pythonxml:latest
Or I could use Powershell (i.e. stop using Git Bash) instead, but I don't like that solution.
Slightly better solution is to use Docker Desktop itself to run the container, and manually specify the volume/mount arguments.
I started realizing the docker + Git Bash was struggling when it logged errors like this:
docker: Error response from daemon: the working directory 'C:/Program Files/Git/app' is invalid, it needs to be an absolute path.
Upvotes: 0
Reputation: 2115
Containers are ephemeral by nature. If you want to retain files generated inside a container you need to mount your host file system to the file system of the container. There are many ways to mount a volume, but a common way is a bind mount
docker run -v /path/to/host:/project sntalarmdocker_snt
Whatever gets saved to /project
will be visible in /path/to/host
even after the container is killed
Upvotes: 5