Docker 和 python 复制指定的文件,解压并执行操作

Docker with python to copy file specified, unzip and do actions

设置

Docker文件(原始,需要修改)

FROM python:3
ADD src/main.py /
RUN chmod +x main.py
RUN /usr/local/bin/python -m pip install --upgrade pip
COPY . /opt/app
RUN pip install -r /opt/app/requirements.txt
ADD / /usr/local
ENTRYPOINT [ "python",  "./main.py" ]

main.py

if __name__ == '__main__':
   if len(sys.argv) == 2:
      main(sys.argv[1])

def main(logs_file_archive):
    unzip_logs(logs_file_archive)  # unzips all logs from the provided path to the folder with the same name as archive/Extracted directory
    create_csv_files()  # creates CSV files needed to plot graph
    process_files()  # populate CSV files generated with the right data
    plot(logs_file_archive)  # build this data representation

Actual/desired 行为

实际:

2022-01-17T22:05:31.547047838Z   File "//./main.py", line 214, in <module>
2022-01-17T22:05:31.547210046Z     main(sys.argv[1])
2022-01-17T22:05:31.547259438Z   File "//./main.py", line 187, in main
2022-01-17T22:05:31.547670294Z     unzip_logs(logs_file_archive)
2022-01-17T22:05:31.547732344Z   File "//./main.py", line 54, in unzip_logs
2022-01-17T22:05:31.548296998Z     with zipfile.ZipFile(file_path, "r") as zip_ref:
2022-01-17T22:05:31.548350898Z   File "/usr/local/lib/python3.10/zipfile.py", line 1240, in __init__
2022-01-17T22:05:31.549638566Z     self.fp = io.open(file, filemode)
2022-01-17T22:05:31.549692977Z FileNotFoundError: [Errno 2] No such file or directory: '/Users/user/PerfReader/misc/archive.zip'

No such file or directory: '/Users/user/PerfReader/misc/archive.zip' 应该很好...因为 Docker 机器中没有这样的文件。

Desired: container runs using Dockerfile, data processes, plot is played real-time or save as a file and transferred to host

Question/issue 描述

  1. 我不完全确定如何将指定的文件传输到 Docker 容器。我阅读了 https://docs.docker.com/storage/volumes/ 但它没有提供任何示例,因此我寻求有关如何安装卷的示例。
  2. 假设我在 main.py 中的 plot() 确实正确地绘制了数据,我在显示这些数据时有哪些选择(整个练习的输出是绘图)?我可以实时显示 Docker 的情节吗?或者我唯一的选择是生成一个图,然后使用 matplotlib.pyplot.savefig?
  3. 将其传回主机

第一个问题:

有多种方法可以访问主机文件。您可以使用 copyadd 命令,就像您在 Docker 文件中所做的那样,该文件将在构建映像期间被复制。此外,您可以使用绑定挂载,它允许您访问绑定的主机目录,就像您是 运行 这个目录中的容器一样。

第二个问题:

Docker 不支持 GUI 或访问主机显示。但是,您可以使用 Xauth 允许它这样做。考虑以下 link 的步骤。

无论如何,我不鼓励使用 Docker 来做你正在做的事情,尤其是绘图部分,python 虚拟环境就足够了。