I want to copy a file generated by a docker container, and store inside it to my local host. What is a good way of doing that? The docker container is ephemeral (i.e. it runs for a very short time and then stops.)
I am working with the below mentioned scripts:
Python (script.py
) which generates and saves a file titled read.txt
.
with open('read.txt', 'w') as file:
lst = ['Some\n',
'random\n',
'sentence\n']
file.writelines("% s\n" % words for words in lst)
I use the below Dockerfile
:
FROM python:3.9
WORKDIR /app
RUN pip install --upgrade pip
COPY . /app/
RUN pip install --requirement /app/requirements.txt
CMD ["python", "/app/script.py.py"]
Below is my folder structure:
- local
- folder1
- script.py
- requirements.txt
- Dockerfile
- folder2
Till now, I have managed to successfully build a docker container using:
docker build --no-cache -t test:v1 .
When I run this docker container inside /local/folder1/
using the below command, I get the desired file, i.e. read.txt
inside /local/folder1/
docker run -v /local/folder1/:/app/ test:v1
But, when I run docker run -v /local/folder2/:/app/ test:v1
inside /local/folder2/
, I do not see read.txt
inside /local/folder2/
and I get the below message.
python: can't open file '/app/script.py': [Errno 2] No such file or directory
I want to be able to get read.txt
inside /local/folder2/
when I run the docker container test:v1
inside /local/folder2/
. How can I do it? I want to be able to do it without copying the contents of /local/folder1/
inside /local/folder2/
.
The docker container is ephemeral (i.e. it runs for a very short time and then stops.) Hence answers given in this and this Stackoverflow posts, which focus on docker cp
have not worked for me.
Time of running of the abovementioned container is not very essential. Even if a workable solution increases the time of running of a container, that is okay.