Tuesday, January 4, 2022

[SOLVED] Locate files written by a python script in a container that is not running anymore

Issue

I have a python script that is dumping a pandas dataframe into a file using :

dataframe.to_csv(file.csv)

After executing the docker container using:

Sudo docker-compose up freqtrade ...(and the rest of the command)

I can't locate the file.csv and my cwd is freqtrade/ but can't access the filesystem in the container.

Also tried to save the file to a path in the ubuntu file system but got the following error:

FileNotFoundError: [Errno 2] No such file or directory: '/home/kato/freqtrade/'


Solution

If the container has been stopped and has not been removed you should still find it using docker ps with -a:

docker ps -a

If you found the container ID, you can then copy files from its filesystem without starting it using docker cp:

docker cp CONTAINER_ID:absolute/path/to/file ./host/target/path

Another solution to avoid doing this in the future, you could run your container with a bind mount (a directory from your machine would be mapped to a directory inside the container):

docker run -v "$(pwd)"/relative/host_dir:/absolute/container_dir my_image


Answered By - AymDev