简体   繁体   English

在 Docker 容器中使用 Python 编写输出

[英]Writing Outputs Using Python In A Docker Container

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).构建 docker 映像 (docker_run_test) 后,我尝试将文本 (.txt) 文件写入 Windows 10 上的本地桌面文件夹。 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/). docker build 似乎可以使用“docker build -t docker_run_test.”无缝运行,从 Dockerfile 和 Python 脚本所在的桌面上的工作目录 (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.该脚本是一个简单的打印语句并将打印语句写入 .txt 文件,但我找不到输出 .txt。 Below is the associated Dockerfile, and the Python script.下面是关联的 Dockerfile 和 Python 脚本。

The Dockerfile: Dockerfile:

FROM python:3

ADD docker_test.py ./

RUN pip install pandas

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

The Python script (docker_test.py): Python 脚本 (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.我也搜索了 Docker 映像的内容,但也找不到映像中的输出 .txt。 Any assistance is most appreciated!非常感谢任何帮助!

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更改输出文件名以写入另一个文件夹,比如/output

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

And then ask Docker to bind host folder %HOMEPATH%\\Desktop to container /output :然后让 Docker 将主机文件夹%HOMEPATH%\\Desktop绑定到容器/output

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

Not sure for %HOMEPATH% syntax as I'm a Linux user.不确定%HOMEPATH%语法,因为我是 Linux 用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM