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!

2
  • 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. Commented Nov 10, 2021 at 3: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 Commented Nov 10, 2021 at 3:42

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.