在 Linux 上運行 IronPDF:設定指南

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPdf for Linux 大約是 280MB(包括 IronPDF 代碼和整個 Chrome 瀏覽器). Docker 映像檔大約是 500MB。

Chrome 還需要一些基本軟件包,這些軟件包根據您的 Linux 發行版而有所不同。 大多數 Linux 發行版已經安裝了這些軟件包,因為它們被 Linux 上的各種應用程序和庫所使用。

然而,如果您使用的是精簡發行版,您將需要安裝在 Linux 上運行 Chrome 所需的 Linux 包。

注意:為了讓除錯更容易,您可以設定以下選項:

IronPdf.Logging.Logger.EnableDebugging = true;
IronPdf.Logging.Logger.LogFilePath = "Default.log";
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
IronPdf.Logging.Logger.EnableDebugging = true;
IronPdf.Logging.Logger.LogFilePath = "Default.log";
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
IronPdf.Logging.Logger.EnableDebugging = True
IronPdf.Logging.Logger.LogFilePath = "Default.log"
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All
VB   C#

如何製作較小的 Docker 映像檔

1. 在執行時安裝套件

減少大小的一種方法是在運行時執行 apt-get 命令,而不是在建立 docker 映像時執行。

  1. 從您的 Dockerfile 中移除 apt-get 命令。

  2. 在初始化IronPdf或渲染文檔之前,設置Installation.LinuxAndDockerDependenciesAutoConfig = true;

  3. 確保您的應用程式具有足夠的權限來執行 apt-get 命令。

    注意:您的首次初始化會較慢,因為 apt-get 命令必須在渲染第一個文件之前完成,而且每次重新部署映像時都會重複這個過程。

    注意:您應該在控制台/日誌中看到指示成功安裝套件的條目:

Executing command 'apt install -y libnss3' in '/app/bin/Debug/netcoreapp3.1/runtimes/linux-x64/native'
    Reading package lists...
    Building dependency tree...
    Reading state information...
    The following additional packages will be installed:
      libnspr4
    The following NEW packages will be installed:
      libnspr4 libnss3
    0 upgraded, 2 newly installed, 0 to remove and 9 not upgraded.

2. 使用 IronPdf.Slim

您也可以選擇_只_包含 IronPdf.dll 並在運行時下載所需文件。

  1. 移除 IronPdf.Linux(以及任何其他的 IronPDF)NuGet 套件(s)從您的專案中加入 IronPdf.Slim

  2. 在初始化 IronPdf 或渲染文件之前,設置 Installation.AutomaticallyDownloadNativeBinaries = true;

  3. 確保您的 dockerfile 授予應用程式整個工作目錄的讀寫權限。(例如,將 RUN chmod +rwx /app/runtimes/linux-x64/native/IronCefSubprocess 更改為 RUN chmod +rwx /app/)

    注意:您首次初始化時會較慢,因為 nuget 包需要下載並在渲染您的第一份文件之前解壓縮,且每次重新部署映像時都會重複此過程。

    注意:您應該在控制台/日志中看到指示成功下載和提取的條目:

Downloading NuGet package from 'https://www.nuget.org/api/v2/package/IronPdf.Native.Chrome.Linux/2023.1.11387'
    Extracting package contents '/app/bin/Debug/netcoreapp3.1/IronPdf.Native.Chrome.Linux.2023.1.11387.nupkg/runtimes/linux-x64/native' to '/app/bin/Debug/netcoreapp3.1/runtimes/linux-x64/native'
    Successfully deployed NuGet package 'IronPdf.Native.Chrome.Linux' to '/app/bin/Debug/netcoreapp3.1/runtimes/linux-x64/native'
    Successfully located 'IronInterop' at '/app/bin/Debug/netcoreapp3.1/runtimes/linux-x64/native'

3. 結論

總結來說,要減少初始容器大小:

  • 使用 IronPdf.Slim NuGet 套件
  • 設定 IronPdf
Installation.LinuxAndDockerDependenciesAutoConfig = true;
    Installation.AutomaticallyDownloadNativeBinaries = true;
  • 確保應用程序目錄具有可寫/可執行的權限。

    RUN chmod +rwx /app/

    一個非常精簡的 Docker 映像檔,同時利用第1點和第2點應該將大小從約500MB減少到約200MB。

    如果可能的話,您將會看到如果不選擇精簡部署,會有最佳的性能表現。

    一些容器化和雲端環境是_非持久性_的,因此可能偶爾需要重新下載依賴項,或者再次運行 apt-get 命令,這可能需要幾分鐘的時間。!

    然而,我們明白有些開發者可能會受到部署文件大小嚴格限制。