在 Docker 容器中运行数据 API 生成器

数据 API 生成器(DAB)作为容器映像发布到Microsoft容器注册表。 任何 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. 创建名为 Library 的新数据库(如果尚不存在)。

    IF NOT EXISTS(SELECT name FROM sys.databases WHERE name = 'Library')
    BEGIN
        CREATE DATABASE Library;
    END
    GO
    
    USE Library
    
  5. 创建包含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
    

创建配置文件

创建映射到前面步骤中创建的表的配置文件。 此配置文件介绍了 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 容器注册表上托管的 Docker 容器映像运行 DAB。 运行容器映像时,装载目录,以便 DAB 可以读取配置文件。

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

    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. 使用 Web 浏览器导航到 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 端口。 反向代理还可用于强制客户端通过 HTTPS 连接到服务器,以确保在将请求转发到容器之前加密通信通道。