docker-compose healthcheck无法以预期的方式使容器先运行然后再运行容器B [英] docker-compose healthcheck does not work in a way it is expected for making container a run first and then container B

查看:47
本文介绍了docker-compose healthcheck无法以预期的方式使容器先运行然后再运行容器B的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用docker compose来运行几个相互依赖的服务.这是docker-compose的一部分:

I am using docker compose to run couple of service which depends on each other. Here is part of the docker-compose:

  backend:
    build: .
    command: bash -c "npm run build && npm start"
    ports: 
      - "3015:3015"
    depends_on:
      - couchdb
      - redis
      - uds-mock-server
    volumes:
      - /app/node_modules
      - .:/app
    user: root
  api-test:
    restart: always 
    build: .
    depends_on:
      - backend
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3015/readiness"]
      interval: 200s
      timeout: 200s
      retries: 5
    user: root

如您所见,我在那边有两个服务,后端应该先运行,并且服务器需要准备就绪,然后才能启动api-test.后端有一个端点:localhost:2015/readiness,只要它返回200,就可以开始api测试.当我在构建订单时运行时,首先要遵循后端,然后是api-mock,但是当docker compose开始运行它们时,api-test运行得更快,并且由于它依赖于后端准备就绪,因此失败了.

As you see I have two service over there and backend should first run and the server needs to be ready then api-test can start. backend has an endpoint: localhost:2015/readiness and whenever it returns 200 then api test can start. When I run while building the order is respected so backend first followed by api-mock but when docker compose starts running them api-test run quicker and since it relies on the backend to be ready it fails.

基于以下内容:

Docker Compose在之前等待容器X从Y开始

在作曲家文件中进行Docker健康检查

建议我应该使用在api测试中执行的healthcheck:

It is suggested that I should use healthcheck which I do in api test:

healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost:3015/readiness"]
  interval: 200s
  timeout: 200s
  retries: 5

如果我正确理解此API,则应运行api-test并调用就绪端点,并等待直到它从后端就绪(最多200秒)听到消息,如果失败,它将等待200秒,然后重试5次.但是我看到的是api-test不断失败并重新启动,甚至没有机会运行后端,并且它像循环一样一直在这样做.我有什么想念的吗?任何帮助都非常感激

If I get this correctly api-test should run and call the readiness endpoint and wait until it hears from backend readiness (up to 200s) if it fails it waits for 200s and then try again for 5 times. But what I see is api-test keep failing and restarting and does not even give a chance to backend to run and it keeps doing this like a loop. Am I missing anything? Any help is really appreciated

推荐答案

这两个示例基于condition 形式/compose-file/#depends_on"rel =" noreferrer> depends_on ,在撰写版本3中不再受支持.因此,除非您的docker-compose版本是< 3,否则 healthcheck 不会对您有太大帮助. healthcheck 设置容器的状态(启动,健康或不健康),但是 docker-compose 不会等到后端 app-test 之前,code>容器是健康的.在控制启动和关闭顺序中,有关于 depends_on 的工作方式的详细说明.在撰写中

The two examples are based on the condition form of depends_on which is no longer supported in compose version 3. So, unless your docker-compose version is <3 the healthcheck will not help you much. The healthcheck sets the status of the container (starting, healthy or unhealthy) but docker-compose does not wait until backend container is healthy before starting the app-test. There is a detailed explanation about how depends_on works in Control startup and shutdown order in Compose

请注意,撰写文件中的运行状况检查会设置 app-test 容器的状态,而不是后端的状态.

As a side note, the healthcheck in your compose file sets the status of the app-test container and not backend.

因此,为了控制 api-test 何时可以启动,您必须包装容器的service命令.对于您的特定情况,以下将完成工作:

Therefore to control when the api-test can start, you have to wrap the service command of the container. For your particular case the following will do the job:

bash -c 'while [[ "$(curl --connect-timeout 2 -s -o /dev/null -w ''%{http_code}'' https://backend:3015/readiness)" != "200" ]]; do echo ..; sleep 5; done; echo backend is up; <service_command>'

它尝试每5秒连接一次后端(连接超时为2s).当收到的HTTP状态代码为200 OK时,循环结束,并执行< service_command>

It tries to connect to backend every 5 seconds (the connection timeout is 2s). When the received HTTP status code is 200 OK the loop ends and it executes the <service_command>

相关的docker-compose部分:

The relevant docker-compose part:

  api-test:
    restart: always
    command: bash -c 'while [[ "$$(curl --connect-timeout 2 -s -o /dev/null -w ''%{http_code}'' uds-mock-server:4000/readiness)" != "200" ]]; do echo ..; sleep 5; done; echo backend is up;npm start'
    depends_on:
      - backend
      ...

希望这会有所帮助.

这篇关于docker-compose healthcheck无法以预期的方式使容器先运行然后再运行容器B的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆