Docker没有将文本文件构建为输出,并且无法搜索文件

时间:2019-01-08 11:43:30

标签: python docker

我有一个正在创建文本文件的python脚本。当我单独运行python脚本时,它工作正常。

class TXT_file():
### initialize the txt_file class
def __init__(self, path2file):
    self.path2file = path2file     
    with open(path2file, "w") as f: 
        f.write("") 

### write sentence to the txt_file
def sentence_to_file(self, sentence):
    with open(self.path2file, "w") as f: 
        f.write(sentence) 

### function initiating the class file and writing "Hello world!"
def run_sequence(path2data, file):
    ### initialize the gps_file class
    my_txt_file = TXT_file(path2data + file)
    my_txt_file.sentence_to_file("Hello world!")

### main
if __name__ == "__main__":
    ### setting up paths to various data files
    path2data = 'C:\\Users\\abc\\folder\\'
    file = "example_text_file.txt"
    run_sequence(path2data, file)

当我用它构建一个docker镜像时。我已经为Windows安装了Docker,并使用Windows容器。 Dockerfile看起来像

FROM python:3
ADD txt_file_creation.py /
CMD [ "python", "./txt_file_creation.py", " ./example_text_file.txt"]

我无法获取文件example_text_file.txt作为输出,并抛出错误:

我正在使用以下命令来构建和运行docker映像:

docker build -t test_print_1 .
docker run test_print_1

我得到的错误是:

Traceback (most recent call last):
  File "./txt_file_creation.py", line 34, in <module>
    run_sequence(path2data, file)
  File "./txt_file_creation.py", line 26, in run_sequence
    my_txt_file = TXT_file(path2data + file)
  File "./txt_file_creation.py", line 15, in __init__
    with open(path2file, "w") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\abc\\folder\\example_text_file.txt'

我该如何在与docker相同的文件夹中打印文件。

0 个答案:

没有答案