0

I am trying to write a text (.txt) file to a local Desktop folder on Windows 10 after building a docker image (docker_run_test). The docker build seems to work seamlessly using "docker build -t docker_run_test .", running the command from the working directory on the Desktop where the Dockerfile and Python script reside (C:/Users/mdl518/Desktop/docker_tests/). The script is a simple print statement and writing of the print statement to a .txt file, but I cannot locate the output .txt. Below is the associated Dockerfile, and the Python script.

The Dockerfile:

FROM python:3

ADD docker_test.py ./

RUN pip install pandas

CMD ["python3","./docker_test.py"]

The Python script (docker_test.py):

import os

print("This is a Docker test.")

with open('docker_test.txt', 'w') as f:
    f.write("This is a Docker test.")

I have searched the contents of the Docker image as well, but cannot locate the output .txt within the image either. Any assistance is most appreciated!

mdl518
  • 327
  • 3
  • 14
  • 1
    Files will be written in the **container**. The container stops when the script ends. You should mount a volume to exchange files with the host. – Klaus D. Nov 10 '21 at 03:40
  • It seems like you need add a command to prevent container to stop, then you can go into the running container to view the file – Minh Tri Le Nov 10 '21 at 03:42

1 Answers1

3

You have to mount/bind the folder where you want to see the results into the container.

Change the output filename to write in another folder, let say /output

with open('/output/docker_test.txt', 'w') as f:

And then ask Docker to bind host folder %HOMEPATH%\Desktop to container /output :

docker run -v %HOMEPATH%\Desktop:/output docker_run_test

Not sure for %HOMEPATH% syntax as I'm a Linux user.

  • Benoit Courty et al. - Thank you for the further clarity, this answered my question perfectly and I am now able to access the output file in the local directory! I will confirm your answer as the correct solution, thanks again! – mdl518 Nov 10 '21 at 21:48