簡體   English   中英

Docker 和 python 復制指定的文件,解壓縮並執行操作

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

設置

  • 項目
    • 源代碼
      • 主文件
    • Dockerfile

Dockerfile(原始,需要改造)

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" ]

主文件

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

實際/期望的行為

實際

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 機器中沒有這樣的文件。

期望:容器使用Dockerfile運行,數據處理,plot實時顯示或保存為文件並傳輸到主機

問題/問題描述

  1. 我不完全確定如何將指定的文件傳輸到 Docker 容器。 我閱讀了 https://docs.docker.com/storage/volumes/但它沒有提供任何示例,因此我尋求如何安裝卷的示例。
  2. 如果我在 main.py 中的 plot() 正確執行 plot 數據,那么我在顯示這些數據時有哪些選擇(整個練習的輸出是繪圖)? 我可以從 Docker 實時顯示 plot 嗎? 或者我唯一的選擇是生成 plot 然后使用matplotlib.pyplot.savefig將其傳輸回主機?

第一個問題:

有多種方法可以訪問主機文件。 您可以使用copyadd命令,就像您在 Docker 文件中所做的那樣,該文件將在構建映像期間被復制。 此外,您可以使用綁定掛載,它允許您訪問已綁定的主機目錄,就像您在此目錄中運行容器一樣。

第二個問題:

Docker 不支持 GUI 或訪問主機顯示。 但是,您可以使用Xauth允許它這樣做。 請考慮以下鏈接的步驟。

無論哪種方式,我都不鼓勵將 Docker 用於您正在做的事情,尤其是繪圖部分,python 虛擬環境就綽綽有余了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM