仅在其他服务完成后才如何启动服务? [英] How to start service only when other service had completed?

查看:111
本文介绍了仅在其他服务完成后才如何启动服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用compose,我只想在另一个服务以代码0退出后才启动服务。



我对此功能有多种需求。基本需求是我在哪里:




  • 数据库服务

  • 数据库迁移服务

  • 应用程序服务



数据库服务将启动一个空数据库。我的应用程序需要在数据库中进行架构设置,因此我有第二项服务,该服务将在运行时执行。完成后,此服务将成功退出-我可以在我的Docker撰写日志文件中看到它:


webservices_kong-migration_1已退出,代码为0


我不希望在 数据库迁移完成之后启动应用程序服务。



我在docker-compose文件中为应用程序服务包含以下内容:


  depends_on:
kong数据库:
条件:service_healthy
kong迁移:
条件:service_started


我知道这是错误的,因为我可以在docker-compose日志中看到该应用程序是在迁移后启动的已经开始,但尚未完成,导致应用程序失败。



(我一直在使用 https:// gith ub.com/Kong/docker-kong/blob/master/compose/docker-compose.yml 作为示例)



docker-compose是否具有功能要这样做还是我应该考虑使用另一种方法?

解决方案

Docker compose还没有开箱即用的解决方案。

但是存在许多解决此问题的好的实现。



更好的解决方案之一是将服务发现用作领事 Joyent Containerpilot 中实现的操作/自动驾驶 rel = noreferrer>自动驾驶模型。 p>

此模型甚至允许在根本不存在服务依赖项的docker swarm集群中实现服务之间的依赖项。



Containerpilot作业模型允许创建简单的脚本来保留启动的服务以正确的顺序。


Using compose I want to start a service only after another service has exited with code 0.

I have mutiple needs for this functionality. The basic need is where I have:

  • database service
  • database migration service
  • application service

The database service will start an empty database. My app needs it's schema setup in the database so I have a second service which will do this when it is run. When it is done this service will exit successfully - I can see this in my docker compose log file:

webservices_kong-migration_1 exited with code 0

I do not want the application service to start until after the database migration has completed.

I have the following in my docker-compose file for the application service:

depends_on:
  kong-database:
    condition: service_healthy
  kong-migration:
    condition: service_started

I know this is wrong because I can see in my docker-compose log that the application is started after the migration has started but before it has completed, causing the application to fail.

(I have been taking https://github.com/Kong/docker-kong/blob/master/compose/docker-compose.yml as an example)

Does docker-compose have functionality to do this or should I be considering another method?

解决方案

Docker compose haven't any out-of-the-box solutions.
But exists a number of good implementations for solving this problem.

One of the better solutions is using service discovery as Consul and Autopilot model implemented in Joyent Containerpilot.

This model allows implementing dependencies between services even in docker swarm cluster where service dependency does not exist at all.

Containerpilot jobs model allow creating simple scripts to keep started services in the right order.

这篇关于仅在其他服务完成后才如何启动服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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