本文介绍了Docker Compose Wait til依赖项容器在启动前已完全启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用docker-compose处理docker服务,并且我有一个依赖于花药的服务.

I'm working with a docker service using docker-compose, and I have a service that depends on anther.

我使用了 depends_on 键,但是具有依赖项的服务在依赖服务完全启动之前启动.

I've used the depends_on key, but the service with the dependency launches prior to the depending service being completely up.

version: '3'

services:
  KeyManager:
    image: cjrutherford/keymanager
    deploy:
      replicas: 1
    ports:
      - '3220:3220'
    networks:
      - privnet
  YellowDiamond:
    image: cjrutherford/server
    depends_on:
      - KeyManager
    deploy:
      replicas: 1
    ports:
      - '3000:3000'
    networks:
      - privnet
      - web
networks:
  privnet:
    internal: true
  web:

这两个都是节点应用程序,要求密钥管理器在服务器启动之前正在运行以接受请求.我可以添加超时吗?或在应用程序中发送触发器?只是从经理那里获取密钥还为时过早.

Both of these are node applications, and the keymanager is required to be running to accept requests before the server launches. Can I add a timeout? or send a trigger in the app? it's just launching way too early to get the key from the manager.

推荐答案

我经常发现使用wait-for-it bash脚本比对docker-compose进行内置运行状况检查要有效得多.

I've often found using a wait-for-it bash script much more effective than the built in health check to docker-compose.

这将针对给定的端口运行TCP运行状况检查,并等到此操作完成后再开始运行进程.

This runs a TCP health check against a given port and waits until this is complete before starting to run a process.

示例代码:

version: "2"
services:
  web:
    build: .
    ports:
      - "80:8000"
    depends_on:
      - "db"
    command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]
  db:
    image: postgres

以下是一些文档:

这篇关于Docker Compose Wait til依赖项容器在启动前已完全启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 13:55