IronPdf.Linux

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 上的庫使用。

然而,如果您使用的是簡化版發行版,您將需要安裝使 Chrome 能在 Linux 上運行所需的 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,並添加 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 命令,可能需要花費幾分鐘。!

然而,我們了解到一些開發人員可能會受到其部署的嚴格大小要求限制。