Home > OS >  Writing Outputs Using Python In A Docker Container
Writing Outputs Using Python In A Docker Container

Time:11-11

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!

CodePudding user response:

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.

  • Related