如何从其他容器访问CosmosDB Docker容器

g52tjvyc  于 2023-03-01  发布在  Docker
关注(0)|答案(2)|浏览(139)

我把两个码头集装箱连接起来:一个包含CosmosDb模拟器映像容器,另一个包含我的函数应用程序。这两个容器都使用Docker Desktop在Linux中运行。
我需要能够从我的函数应用程序容器访问cosmosDb容器,结果发现这是个问题,我花了几天时间才解决,所以我把解决方案放在这里。
Microsoft指南没有解释如何执行此操作。它实际上是告诉您如何从本地计算机访问CosmosDb容器。https://learn.microsoft.com/en-us/azure/cosmos-db/docker-emulator-linux?tabs=sql-api%2Cssl-netstd21
cosmsdb仿真器使用的证书总是具有localhost域,并且不能将其配置为容器主机名:https://本地主机:8081/资源管理器/模拟器. pem
因此,如何从我的函数应用容器访问Cosmos容器?

myzjeezk

myzjeezk1#

我发现解决方案是使用固定的ip配置CosmosDb容器,然后我可以使用http://:8081访问CosmosDb示例。<ip_address>:8081.
这里的关键是给cosmos容器一个固定的ip(在我的例子中是www.example.com),你可以用它来获得上面的microsoft指南中的证书,然后你可以从你的应用程序中调用它,记住设置你的HOST_IP变量,这样你就可以从你的本地pc访问它。172.16.238.246) which you can use to get the cert as per the microsoft guide above. You can then call it from your application. Remember to set your HOST_IP variable so you can access it from your local pc.
Docker组成:

networks:
  default:
    external: false
    ipam:
      driver: default
      config:
        - subnet: "172.16.238.0/24"

services:
  cosmosDb:
    container_name: CosmosDb
    image: "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator"
    tty: true
    restart: always
    mem_limit: 3G
    cpu_count: 4
    environment:
      - AZURE_COSMOS_EMULATOR_PARTITION_COUNT=5
      - AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true
      # Set this as an environment variable in host shell to allow access from your local pc using http://locahost:8081 e.g. $Env:HOST_IP="192.168.0.16"
      - AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE=${HOST_IP}
    ports:
      - '8081:8081'
      - '10250-10255:10250-10255'
    networks:
      default:
        ipv4_address: 172.16.238.246
    healthcheck:
      test: ["CMD", "curl", "-fk", "https://localhost:8081/_explorer/emulator.pem"]
      interval: 5s
      timeout: 10s
      retries: 5
      start_period: 20s
    entrypoint: ["/bin/bash","-c"]
    command: 
       - |
          apt-get update -y && apt-get install -y curl
          /usr/local/bin/cosmos/start.sh

  myFuncApp:
    build:
      dockerfile: Dockerfile
      context: .
    container_name: MyFuncApp
    image: myfuncapp
    ports:
      - '80:80'    
    entrypoint: ["/bin/bash","-c"]
    command: 
       - |
        curl -fk -o ~/cosmosemulatorcert.crt https://172.16.238.246:8081/_explorer/emulator.pem
        cp ~/cosmosemulatorcert.crt /usr/local/share/ca-certificates/
        update-ca-certificates
        /azure-functions-host/Microsoft.Azure.WebJobs.Script.WebHost
    depends_on:
      cosmosDb: 
        condition: service_healthy

停靠文件:

FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4.0-dotnet-isolated6.0 AS base

FROM base AS final

COPY bin/Debug/net6.0 /home/site/wwwroot

RUN apt-get update -y && apt-get install -y curl

ENV CosmosDb__EndpointUrl="https://172.16.238.246:8081"
6qftjkof

6qftjkof2#

如果有帮助的话,我发现我可以在Docker编写文件中使用Azure Cosmos DB模拟器的服务名来引用它。
下面是一个例子:

version: '3'
services:
  devenv:
    image: mcr.microsoft.com/dotnet/sdk:6.0
    command: sleep infinity
    volumes:
      - ..:/workspace
    depends_on:
      - azurecosmosdb
  azurecosmosdb:
    image: mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator
    ports:
      - 8081:8081

然后,您可以使用服务的名称和端口号引用模拟器。下面是创建CosmosClient的示例。

  • 此处使用的密钥是模拟器的默认密钥。*
CosmosClient client = new ("AccountEndpoint=https://azurecosmosdb:8081;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==;");

相关问题