【问题标题】:How to verify the result of a console application in healthcheck of docker compose?如何在 docker compose 的运行状况检查中验证控制台应用程序的结果?
【发布时间】:2021-07-01 12:11:18
【问题描述】:

我正在使用以下 docker-compose 文件来运行 3 个项目。

  1. 我的 api 应用程序
  2. 用于执行数据库迁移的控制台应用程序
  3. 一个运行单元测试的测试项目。

它们都在同一个解决方案中,因此使用相同的 docker 文件。 我想确保只有在迁移项目成功完成后才能运行测试项目。如何在 docker compose 文件中向控制台应用程序 (myserver-migrations) 添加运行状况检查? 现在我刚刚添加了

depends_on:
      - myserver-migration

但这并不能保证迁移成功。有没有办法在控制台应用程序中设置一些状态并在另一个(单元测试)容器中读取它?

services:
  myserver:
    container_name: myserver
    ports:
      - "3434:80"
    build:
      context: .
      dockerfile: Dockerfile
      target: test
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/health"]
      interval: 20s
      timeout: 20s
      retries: 1
    depends_on:
      postgres:
        condition: service_healthy
  myserver-migration:
    container_name: myserver-migration
    build:
      context: .
      dockerfile: Dockerfile
      target: build    
    depends_on:
      postgres:
        condition: service_healthy
      myserver:
        condition: service_healthy
    command: 'dotnet run -p MyServer.Migrations resetdb true'
myserver-tests:
container_name: myserver-tests
build:
  context: .
  dockerfile: Dockerfile
  target: build
depends_on:
  - myserver-migration

command: 'dotnet test MyServer.sln'

【问题讨论】:

    标签: .net docker .net-core docker-compose


    【解决方案1】:

    通过在条件中使用service_completed_successfully 标志修复了该问题。

    myserver-tests:
    container_name: myserver-tests
    build:
      context: .
      dockerfile: Dockerfile
      target: build
    depends_on:
      myserver-migration:
        condition: service_completed_successfully
    

    service_completed_successfully确保依赖容器的退出代码为0。所以我修改了控制台应用程序,如果一切按计划执行,则返回0。

    【讨论】:

      猜你喜欢
      • 2020-08-20
      • 2019-09-08
      • 2019-01-30
      • 2010-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      • 1970-01-01
      相关资源
      最近更新 更多