Dockerfile包含一个写入输出文件的python脚本,但是输出文件没有在容器上创build

这是我的第一个问题,所以要温柔:)

我有一个Dockerfile如下:

FROM centos:latest Maintainer Liz Miller LABEL description="Image Built with Dockerfile." RUN yum -y update RUN yum -y install python-setuptools RUN easy_install supervisor RUN mkdir -p /var/log/supervisor RUN yum -y install which RUN yum -y install git RUN yum install python COPY myscript.py myscript.py CMD ["python", "/myscript.py"] 

而myscript.py python脚本是:

 text_file = open('output.txt', 'w') text_file.write('Hello World') text_file.close() 

当我从这个Dockerfile构build图像时,图像正在使用脚本构build并运行,我没有看到output.txt。 这只是没有被创build。 什么不见​​了? 我如何解决这个问题?

  1. 将您的text_file更改为/log/output.txt

  2. 构build映像不运行CMD。

  3. 在你build立图像后,你运行你的容器,做

     docker run -it --rm -v $(pwd)/log:/log imagename 

你的脚本写在容器内,然后退出。 所以你不会在你的当前目录中看到它。 解决这个问题的方法是将一个主机目录挂载到容器中,然后让脚本写入其中,以便数据可以持久化。

所以我设法解决了这个问题。 CMD确实不运行命令,但运行。

我已经把我的CMD行改为这行:RUN python myscript.py

这解决了我的问题,我能够看到输出文件从python脚本,在构build后的容器。