共用方式為


在 Docker 容器中執行資料 API 產生器

數據 API 產生器 (DAB) 會發佈為 Microsoft Container Registry 的容器映像。 任何 Docker 主機都可以提取容器映像,並以最少的組態執行 DAB。 本指南使用容器映像和本機組態檔快速裝載和執行 DAB,而不需要安裝任何額外的工具。

必要條件

建立範例資料

針對此簡短指南,具有一些數據列的簡單數據表就足以示範如何在 Docker 容器中使用 DAB。 為了進一步簡化工作,我們在 Docker 容器映像中使用適用於 Linux 的 SQL Server。

  1. mcr.microsoft.com/mssql/server:2022-latest提取容器映像。

    docker pull mcr.microsoft.com/mssql/server:2022-latest
    
  2. 執行容器映像發佈埠, 1433 並將帳戶密碼設定 sa 為您在本指南中使用的唯一密碼。

    docker run \
        --name mssql \
        --publish 1433:1433 \
        --detach \
        --env "ACCEPT_EULA=Y" \
        --env "MSSQL_SA_PASSWORD=<your-password>" \
        mcr.microsoft.com/mssql/server:2022-latest
    

    重要

    這是本指南的簡單虛構密碼。 在真實世界中,您會使用不同的驗證機制,並在理想情況下使用不同的帳戶。

  3. 使用您慣用的用戶端或工具連線到 SQL Server。 連接字串為 Server=localhost,1433;User Id=sa;Password=<your-password>;TrustServerCertificate=true;

  4. 如果尚未存在,Create 名為 Library 的新資料庫。

    IF NOT EXISTS(SELECT name FROM sys.databases WHERE name = 'Library')
    BEGIN
        CREATE DATABASE Library;
    END
    GO
    
    USE Library
    
  5. Create 具有idtitleyearpages 數據行的Books數據表。

    DROP TABLE IF EXISTS dbo.Books;
    
    CREATE TABLE dbo.Books
    (
        id int NOT NULL PRIMARY KEY,
        title nvarchar(1000) NOT NULL,
        [year] int null,
        [pages] int null
    )
    GO
    
  6. 將四個範例書籍數據列插入 Books 數據表中。

    INSERT INTO dbo.Books VALUES
        (1000, 'Practical Azure SQL Database for Modern Developers', 2020, 326),
        (1001, 'SQL Server 2019 Revealed: Including Big Data Clusters and Machine Learning', 2019, 444),
        (1002, 'Azure SQL Revealed: A Guide to the Cloud for SQL Server Professionals', 2020, 528),
        (1003, 'SQL Server 2022 Revealed: A Hybrid Data Platform Powered by Security, Performance, and Availability', 2022, 506)
    GO
    
  7. 使用簡單的 SELECT * 查詢來測試您的數據。

    SELECT * FROM dbo.Books
    

建立組態檔

Create 對應至先前步驟中建立之數據表的組態檔。 此組態檔描述 DAB 如何將 REST 和 GraphQL 端點對應至實際數據。

  1. 建立名為 dab-config.json 的檔案。

    提示

    這是組態檔的預設檔名。 藉由使用預設檔名,您可以避免在執行容器時指定組態檔。

  2. 將此 JSON 內容新增至您的檔案。 此組態會建立對應 book 至現有 dbo.Books 數據表的單一實體。

    {
      "$schema": "https://github.com/Azure/data-api-builder/releases/latest/download/dab.draft.schema.json",
      "data-source": {
        "database-type": "mssql",
        "connection-string": "Server=host.docker.internal\\mssql,1433;Initial Catalog=Library;User Id=sa;Password=<your-password>;TrustServerCertificate=true;"
      },
      "runtime": {
        "rest": {
          "enabled": true
        },
        "graphql": {
          "enabled": true
        }
      },
      "entities": {
        "book": {
          "source": "dbo.Books",
          "permissions": [
            {
              "actions": [
                "read"
              ],
              "role": "anonymous"
            }
          ]
        }
      }
    }
    

提取並執行 Docker 容器映像

使用裝載在 Microsoft Container Registry 上的 Docker 容器映射來執行 DAB。 執行容器映射時,請掛接目錄,讓 DAB 可以讀取組態檔。

  1. mcr.microsoft.com/azure-databases/data-api-builder提取 Docker 容器映像。

    docker pull mcr.microsoft.com/azure-databases/data-api-builder
    
  2. 執行發佈埠並 5000 系結掛接檔案的 dab-config.json 容器。

    docker run \
        --name dab \
        --publish 5000:5000 \
        --detach \
        --mount type=bind,source=$(pwd)/dab-config.json,target=/App/dab-config.json,readonly \
        mcr.microsoft.com/azure-databases/data-api-builder
    
  3. 使用網頁瀏覽器瀏覽至 http://localhost:5000/api/book。 輸出應該是 REST API 端點中書籍專案的 JSON 陣列。

    {
      "value": [
        {
          "id": 1000,
          "title": "Practical Azure SQL Database for Modern Developers",
          "year": 2020,
          "pages": 326
        },
        {
          "id": 1001,
          "title": "SQL Server 2019 Revealed: Including Big Data Clusters and Machine Learning",
          "year": 2019,
          "pages": 444
        },
        {
          "id": 1002,
          "title": "Azure SQL Revealed: A Guide to the Cloud for SQL Server Professionals",
          "year": 2020,
          "pages": 528
        },
        {
          "id": 1003,
          "title": "SQL Server 2022 Revealed: A Hybrid Data Platform Powered by Security, Performance, and Availability",
          "year": 2022,
          "pages": 506
        }
      ]
    }
    

    注意

    本指南使用 HTTP 連線。 在 Docker 中執行資料 API 產生器容器時,您會看到只有 HTTP 端點已對應。 如果您想要 Docker 容器支援 HTTPS 進行本機開發,您必須提供 SSL/TLS 憑證和 SSL/TLS 加密所需的私鑰檔案,並公開 HTTPS 連接埠。 反向 Proxy 也可以用來強制用戶端透過 HTTPS 連線到您的伺服器,以確保通道在將要求轉送至容器之前已加密。