Run IronPDF as a Remote Container

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

IronPdfEngine 是一個獨立的服務,可以處理 PDF 的創建、寫入、編輯和閱讀。 IronPDF Docker 已準備好運行與 IronPDF 相容版本的 Docker 服務(v2023.2.x 及以上)。 這將幫助開發人員消除他們可能在使用 IronPDF 遇到的部署問題。

為什麼將 IronPDF 作為其自己的容器運行是一個好主意

IronPDF 需要 Chrome 和 Pdfium 二進位文件才能運行,而這些文件的大小都很大(幾百 MB)。 它還需要在機器上安裝多個相依性。

通過使用這種方法,您的客戶端將僅佔用一小部分大小(以 MB 計)。

避免部署問題

配置環境/容器以正確包含所有相依性可能具有挑戰性。 使用 IronPDF Docker 容器意味著 IronPDF 預先安裝並保證可用,避免了所有的部署和相依性問題。

版本

IronPDF Docker 標籤基於 IronPdfEngine 本身的版本。 它與 IronPDF 產品版本不同。

每個 IronPDF 版本都有其相關聯的 IronPdfEngine 版本。 版本號必須與 IronPDF Docker 版本相符。

例如,IronPDF for Java 版本 2023.2.1 需要 IronPdfEngine 版本 2023.2.1。 您不能使用不匹配的 IronPdfEngine 和 IronPDF 版本。


如何使用 IronPDF Docker

安裝 IronPDF

將 IronPdf.Slim Nuget 套件添加到您的專案。

https://www.nuget.org/packages/IronPdf.Slim/

注意:IronPdfIronPdf.LinuxIronPdf.MacOs 套件均包含 IronPdf.Slim。

為了減少應用程序大小,我們建議安裝 IronPdf.Slim。 不再使用 Package IronPdf.Native.Chrome.xxx,因此可以將其從項目中刪除。

確定所需的容器版本

預設情況下,Docker 版的 IronPDF 版本將與 NuGet 上的 IronPDF 的當前版本相匹配。 您可以使用下面的代碼手動檢查版本:

:path=/static-assets/pdf/content-code-examples/how-to/ironpdfengine-docker-version.cs
string ironPdfEngineVersion = IronPdf.Installation.IronPdfEngineVersion;
Dim ironPdfEngineVersion As String = IronPdf.Installation.IronPdfEngineVersion
$vbLabelText   $csharpLabel

設置 IronPDF 為 Docker 容器

不使用 Docker Compose

使用上一步中的版本運行 docker 容器。

  • 必須安裝 Docker。

設置

  1. 轉到 https://hub.docker.com/r/ironsoftwareofficial/ironpdfengine
  2. 提取最新的 ironsoftwareofficial/ironpdfengine 映像
docker pull ironsoftwareofficial/ironpdfengine
docker pull ironsoftwareofficial/ironpdfengine
SHELL

或者提取具體版本(推薦)

docker pull ironsoftwareofficial/ironpdfengine:2025.3.6
docker pull ironsoftwareofficial/ironpdfengine:2025.3.6
SHELL
  1. 運行 ironsoftwareofficial/ironpdfengine 容器。

此命令將創建一個容器並在背景中運行,端口為 33350

docker run -d -p 33350:33350 -e IRONPDF_ENGINE_LICENSE_KEY=MY_LICENSE_KEY ironsoftwareofficial/ironpdfengine:2025.3.6
docker run -d -p 33350:33350 -e IRONPDF_ENGINE_LICENSE_KEY=MY_LICENSE_KEY ironsoftwareofficial/ironpdfengine:2025.3.6
SHELL

使用 Docker Compose

關鍵是設置一個允許 IronPdfEngine 和您的應用程序彼此通信的 Docker 網絡。 設置 'depends_on' 以確保 IronPdfEngine 在應用程序啟動之前上線。

設置

1.首先創建一個 docker-compose.yml 文件。使用以下模板設置Docker Compose 文件:

version: '3.6'
services:
  myironpdfengine:
    container_name: ironpdfengine
    image: ironsoftwareofficial/ironpdfengine:latest
    ports:
      - '33350:33350'
    networks:
      - ironpdf-network
  myconsoleapp:
    container_name: myconsoleapp
    build:
      # enter YOUR project directory path here
      context: ./MyConsoleApp/
      # enter YOUR dockerfile name here, relative to project directory
      dockerfile: Dockerfile
    networks:
      - ironpdf-network
    depends_on:
      myironpdfengine:
        condition: service_started
networks:
  ironpdf-network: 
    driver: 'bridge'
  1. 將 IronPdfEngine 的地址設置在您的應用 (myconsoleapp) 中為 "myironpdfengine:33350"
  2. 運行 docker compose
docker compose up --detach --force-recreate --remove-orphans --timestamps
docker compose up --detach --force-recreate --remove-orphans --timestamps
SHELL

連接到 IronPdfEngine

運行您的 IronPDF 代碼; 您的應用程式現在與 Docker 的 IronPdfEngine 進行通信!

:path=/static-assets/pdf/content-code-examples/how-to/ironpdfengine-docker-use.cs
using IronPdf;
using IronPdf.GrpcLayer;

// Configure for Docker container
var config = IronPdfConnectionConfiguration.Docker;
config.Host = "localhost";
IronPdf.Installation.ConnectToIronPdfHost(config);

// Use IronPDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello IronPDF Docker!<h1>");
pdf.SaveAs("ironpdf.pdf");
Imports IronPdf
Imports IronPdf.GrpcLayer

' Configure for Docker container
Private config = IronPdfConnectionConfiguration.Docker
config.Host = "localhost"
IronPdf.Installation.ConnectToIronPdfHost(config)

' Use IronPDF
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello IronPDF Docker!<h1>")
pdf.SaveAs("ironpdf.pdf")
$vbLabelText   $csharpLabel

連接類型

您可以根據您想要進行的連接類型指定多個 IronPdfConnectionType

以下是可用屬性的列表:

本地可執行:要連接到在本地機器上運行可執行文件的 IronPdfEngine“server”,我們使用此選項。 一個簡單的例子是一個 WinForm 發票應用,它在本地生成 PDF,而不依賴於雲服務。

Docker:此選項應該在嘗試本地或雲端連接到 Docker 容器時使用。

遠程服務器:此選項用於雲端的 IronPdfEngine。 這會通過 HTTP 或 HTTPS 協議連接到雲託管 (例如 Docker) IronPdfEngine 實例。 請注意,因為這是連接到遠程服務器,所以需要完整的 URL(包括 HTTP 或 HTTPS 協議)。

自定義:要對連接進行完全控制和自定義,您可以使用此選項。 此選項使用您自定義定義的 Grpc.Core.ChannelBase,而不是上述其他定義選項。 開發人員可以通過創建新的 Grpc.Core.Channel 對象或使用 Grpc.Net.Client.GrpcChannel.ForAddress(System.String) 來創建新的通道以完全控制gRPC 通道。

.NET Framework 與 NetFrameworkChannel

對於 .NET Framework,我們需要不同的設置,因為 gRPC 在 .NET Framework 項目中工作方式不同。 要使該方法生效,請確保安裝了 Grpc.Core NuGet 套件。 我們將使用從 Grpc.Core.ChannelBase 衍生出的客製 gRPC 通道進行此特殊設置。

讓我們來看看這個例子,在這裡我們將實現連接通道以使用 IronPDFEngine 創建和保存 PDF。

提示由於 gRPC 在 .NET Framework 項目中工作不同,如果以下代碼不起作用,請嘗試在地址中刪除 <http><https> 前綴。

警告請注意,此處需要 pdf.Dispose

:path=/static-assets/pdf/content-code-examples/how-to/ironpdfengine-docker-use-grpc.cs
using IronPdf;

// This code demonstrates how to use IronPdf with gRPC in a .NET Framework application.

// 1. Configure connection to use local IronPdfEngine executable
var config = IronPdf.GrpcLayer.IronPdfConnectionConfiguration.Executable;

// 2. Connect to the IronPDF host with the executable configuration
IronPdf.Installation.ConnectToIronPdfHost(config);

// 3. Create a PDF renderer instance
ChromePdfRenderer renderer = new ChromePdfRenderer();

// 4. Render HTML string as PDF document
PdfDocument pdf = renderer.RenderHtmlAsPdf("Hello world");

// 5. Save the PDF to disk
pdf.SaveAs("output.pdf");

// 6. Clean up , this is needed to work
pdf.Dispose();
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

使用 WithCustomChannel 的替代方法

一個替代的方法是利用 IronPdf.GrpcLayer 提供的 WithCustomChannel 方法。

WithCustomChannel 方法將接受兩個參數,customChannel 是您自定義的 gRPC 通道,metadatametadata 參數是可選的,默認設置為 null

:path=/static-assets/pdf/content-code-examples/how-to/ironpdfengine-docker-use-grpc-alt.cs
using IronPdf;
using IronPdf.GrpcLayer;
using Grpc.Core;

// 1. Create custom gRPC channel (.NET Framework style)
var channel = new Channel("123.456.7.8:80", ChannelCredentials.SecureSsl);

// 2. (Optional) Add metadata headers if needed
var metadata = new Metadata
{
    { "Authorization", "Bearer your_token_here" }
};

// 3. Configure IronPDF with custom channel
var config = IronPdfConnectionConfiguration.WithCustomChannel(channel, metadata);
IronPdf.Installation.ConnectToIronPdfHost(config);

// 4. Generate PDF
var renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("Hello world");

// 5. Save the PDF to disk
pdf.SaveAs("output.pdf");

// 6. Clean up , this is needed to work
pdf.Dispose();
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

在 AWS ECS 上部署 IronPdfEngine

預備知識

設置

  1. 創建一個 ECS 集群。 按照此指南 使用控制台為 Fargate 和 External 啟動類型創建集群
  2. 創建任務定義。 按照指導創建 使用控制台創建任務定義

推薦設置:

  • AWS Fargate
  • 建議最少 1 vCPU 和 2 GB 的 RAM。 根據您的工作負載,如果您滿足具有超過 10 頁的 PDF 或遇到繁重的負載請求,請選擇更高級別。
  • 網絡模式:awsvpc
  • 端口映射
    {
      "containerPort": 33350,
      "hostPort": 33350,
      "protocol": "tcp",
      "appProtocol": "grpc"
    }
  • 映像 URI :指向我們的任何 IronPdfEngine。 例如,“ironsoftwareofficial/ironpdfengine:2024.1.20”(來自 DockerHub)
  • AWS 許可權 & 網絡由您決定
  • 啟用 Amazon CloudWatch 是推薦項。 (啟用日誌記錄)
  • 容器啟動次序 是在同一任務定義中部署應用程序容器時必須的。
  1. 運行任務定義。 您可以將任務定義作為 任務服務 運行。 遵循該指南創建 使用控制台創建新服務

推薦設置:

  • 啟動類型:AWS Fargate
  • 公共 IP:在測試中開啟在生產中關閉。 安全和 AWS 網絡由您決定。
  1. 享受吧! IronPdfEngine Docker 在您的 AWS 上已啟動並運行!

請注意不支持水平方向擴展。 請參閱 IronPdfEngine 限制 了解更多信息。


在 Azure 容器實例上部署 IronPdfEngine

預備知識

設置

  1. 創建一個 Azure 容器。 按照此 利用 Azure 入口網站部署容器實例的快速啟動指南

推薦設置:

  • 映像來源: 其他註冊表
  • 映像: ironsoftwareofficial/ironpdfengine:2024.1.20(來自 Docker Hub)
  • 作業系統類型: Linux
  • 大小:最少 1 vCPU 和 2 GiB 的記憶體,或者更高
  • 端口: TCP 端口 33350
  1. 享受吧! IronPdfEngine docker 在您的 Azure 容器實例中已啟動並運行!

請注意不支持水平方向擴展。 請參閱 IronPdfEngine 限制 了解更多信息。


從 AWS ECR 公共畫廊獲得 IronPdfEngine

先決條件

  • 必須安裝 Docker。

設置

  1. 轉到 https://gallery.ecr.aws/v1m9w8y1/ironpdfengine
  2. 提取 v1m9w8y1/ironpdfengine 映像
docker pull https://gallery.ecr.aws/v1m9w8y1/ironpdfengine
docker pull https://gallery.ecr.aws/v1m9w8y1/ironpdfengine
SHELL

或者提取具體版本(推薦)

docker pull https://gallery.ecr.aws/v1m9w8y1/ironpdfengine:2023.12.6
docker pull https://gallery.ecr.aws/v1m9w8y1/ironpdfengine:2023.12.6
SHELL
  1. 運行 ironpdfengine 容器。

此命令將創建一個容器並在背景中運行,端口為 33350

docker run -d -p 33350:33350 ironsoftwareofficial/ironpdfengine
docker run -d -p 33350:33350 ironsoftwareofficial/ironpdfengine
SHELL

學習如何配置 IronPdf 客戶端使用 IronPdfEngine,請導航至 '更新代碼以使用 IronPdfEngine' 部分。


從市集獲得 IronPdfEngine

為了助您快速上手,我們已在 Azure 和 AWS 市場中設置了 IronPdfEngine。

Azure Marketplace

class="content-img-align-center">
class="center-image-wrapper"> Azure Marketplace

設置

  1. 轉到 Azure Marketplace 上的 IronPDF Docker 容器。 點選“立即獲取”和“繼續”。
  2. 完成“基礎”、“群集細節”及“應用細節”以創建 Kubernetes 服務。
  3. 部署完成後,導航到左側邊欄並選擇 Kubernetes 資源 > 執行命令。 運行以下命令:
kubectl get services
kubectl get services
SHELL
class="content-img-align-center">
class="center-image-wrapper"> Kubernetes 服務 - 運行命令

利用 EXTERNAL-IP 和 PORT(S) 的信息,您可以相應地配置 IronPDFEngine 連接。

:path=/static-assets/pdf/content-code-examples/how-to/pull-run-ironpdfengine-azure-marketplace.cs
using IronPdf;
using IronPdf.GrpcLayer;

IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01";

IronPdfConnectionConfiguration configuration = new IronPdfConnectionConfiguration();
configuration.ConnectionType = IronPdfConnectionType.RemoteServer;
configuration.Host = "http://48.216.143.233";
configuration.Port = 80;
IronPdf.Installation.ConnectToIronPdfHost(configuration);

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>");
pdf.SaveAs("output.pdf");
Imports IronPdf
Imports IronPdf.GrpcLayer

IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"

Dim configuration As New IronPdfConnectionConfiguration()
configuration.ConnectionType = IronPdfConnectionType.RemoteServer
configuration.Host = "http://48.216.143.233"
configuration.Port = 80
IronPdf.Installation.ConnectToIronPdfHost(configuration)

Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>")
pdf.SaveAs("output.pdf")
$vbLabelText   $csharpLabel

AWS Marketplace

class="content-img-align-center">
class="center-image-wrapper"> aws marketplace

預備知識

  • 必須安裝 Docker。
  • 必須安裝並登錄 AWS CLI。

設置

  1. 轉到 AWS 市場上的 IronPdfEngine。 點選 '繼續訂購'。

  2. 接受條款。
class="content-img-align-center">
class="center-image-wrapper"> 接受 EULA
  1. 繼續配置。
class="content-img-align-center">
class="center-image-wrapper"> 訂閱完成
  1. 提取 ironpdfengine 映像。 此步驟將向您展示一個提取 ironpdfengine 映像的命令。
class="content-img-align-center">
class="center-image-wrapper"> 啟動此軟件

例如:

aws ecr get-login-password \
    --region us-east-1 | docker login \
    --username AWS \
    --password-stdin 000000000000.dkr.ecr.us-east-1.amazonaws.com
CONTAINER_IMAGES="000000000000.dkr.ecr.us-east-1.amazonaws.com/iron-software/ironpdfengine:2024.1.15"    
for i in $(echo $CONTAINER_IMAGES | sed "s/,/ /g"); do docker pull $i; done
aws ecr get-login-password \
    --region us-east-1 | docker login \
    --username AWS \
    --password-stdin 000000000000.dkr.ecr.us-east-1.amazonaws.com
CONTAINER_IMAGES="000000000000.dkr.ecr.us-east-1.amazonaws.com/iron-software/ironpdfengine:2024.1.15"    
for i in $(echo $CONTAINER_IMAGES | sed "s/,/ /g"); do docker pull $i; done
SHELL
  1. 運行 ironpdfengine 容器。 此命令將創建一個容器並在背景中運行,端口為 33350.
docker run -d -p 33350:33350 000000000000.dkr.ecr.us-east-1.amazonaws.com/iron-software/ironpdfengine:2024.1.15
docker run -d -p 33350:33350 000000000000.dkr.ecr.us-east-1.amazonaws.com/iron-software/ironpdfengine:2024.1.15
SHELL

IronPdfEngine 的健康檢查

檢查 Docker 容器的健康狀況對於確保在生產環境中的可靠性和可用性至關重要。 檢查 IronPdfEngine Docker 容器的能力允許開發人員在失敗時重啟服務,並在需求增加時擴大資源,同時監控持續應用。

要檢查 IronPdfEngine 的健康狀況,我們可以向 IronPdfEngine 的同一端口發送 gRPC請求(預設情況下為 33350)以驗證是否收到響應。

使用 gRPC 檢查健康狀況

IronPdfEngine 遵循標準的 gRPC 健康檢查模式,利用以下協議結構。

message HealthCheckRequest {
 string service = 1; // Name of the service to check (e.g., "IronPdfEngine")
}

由於檢查的是 IronPdfEngine,我們將服務名稱替換為 IronPdfEngine

以下是使用 JavaScript 與 Postman 發送 gRPC 請求至本地 IronPdfEngine 服務的預設端口 33350 的範例。

class="content-img-align-center">
class="center-image-wrapper"> Status

從響應內容可以看出,狀態響應為 SERVING,表示服務正在運行。 如果容器不健康,狀態響應將為 NOT_SERVING

使用 Kubernetes 設置檢查健康狀況

對於 Kubernetes 設置,我們可以使用以下方法來檢查服務是否健康。

 生存探測:
          執行:
            命令:
            - /bin/grpc_health_probe
            - -addr=:33350
            - -rpc 超時=5s
 生存探測:
          執行:
            命令:
            - /bin/grpc_health_probe
            - -addr=:33350
            - -rpc 超時=5s
YAML

常見問題解答

如何使用 Docker 在 C# 中將 HTML 轉換為 PDF?

您可以使用 IronPDF 的 Docker 設置在 C# 中將 HTML 轉換為 PDF。在 Docker 容器中部署 IronPdfEngine 並使用 RenderHtmlAsPdf 等方法來實現此目的。

使用 IronPdfEngine 在 Docker 中有什麼好處?

使用 IronPdfEngine 在 Docker 中提供了一個預安裝和可靠的環境,減少了依賴性問題,並確保所有必要的組件(如 Chrome 和 Pdfium 二進制文件)都可用。

如何使用 Docker Compose 設置 IronPdfEngine?

創建一個 docker-compose.yml 文件並定義服務和網絡。使用 depends_on 指令確保 IronPdfEngine 在您的應用程序之前啟動。

部署 IronPdfEngine 在 AWS ECS 需要哪些步驟?

要在 AWS ECS 上部署,創建一個 ECS 集群,定義任務定義,並使用 AWS Fargate 進行資源分配。然後,運行您的任務或服務以啟動 IronPdfEngine。

如何從 AWS ECR 公共畫廊拉取 IronPdfEngine?

訪問 AWS ECR 公共畫廊,找到 IronPdfEngine 圖像,並按照提供的命令拉取並運行容器。

是否可以從 Azure Marketplace 使用 IronPdfEngine?

是的,IronPdfEngine 可在 Azure Marketplace 中找到。您可以按照 Marketplace 頁面上的設置說明來部署 Docker 容器。

使用 IronPdfEngine 時我應該注意哪些限制?

IronPdfEngine 不支持水平擴展。在計劃部署時,必須考慮這一限制以確保滿足應用程序的需求。

如何高效處理 Docker 的 PDF 生成和操作任務?

部署 IronPdfEngine 在 Docker 容器中,以高效管理 PDF 的生成和操作任務,利用 Docker 的能力來處理依賴性並確保一致的環境。

在 Azure Container Instances 部署 IronPdfEngine 的要求是什麼?

要在 Azure Container Instances 上部署,確保您擁有 Azure 帳戶並拉取 IronPdfEngine Docker 圖像以進行部署。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布