简体   繁体   English

Docker组成当另一个停止时启动容器

[英]Docker compose to start container when another stops

I'm wondering, is there any option to start containers one by one in Docker Compose? 我想知道,是否可以在Docker Compose中一个一个地启动容器? Eg I want to start DB container and special container to init this DB, and main application should be started after init container finished. 例如,我想启动数据库容器和特殊容器来初始化该数据库,并且应在初始化容器完成后启动主应用程序。 Or I want to perform compilation in one container, then packing in another. 或者我想在一个容器中执行编译,然后在另一个容器中进行打包。 In Kubernetes there is something called "init pods", similar to this, as far as I understand. 据我所知,在Kubernetes中有一个叫做“ init pods”的东西。 Is there any alternative in Docker Compose? Docker Compose中还有其他选择吗?

What you are trying to do here fundamentally does not refer to as service. 您在此处尝试做的事情基本上不会称为服务。 As detailed in comment , service is a long-running process that interacts with other services to form an application. 注释中所述 ,服务是一个长期运行的过程,与其他服务交互以形成应用程序。

I suggest you take a look at dobi It runs project tasks (like running a shell, running unit tests, running migration). 我建议您看看dobi它运行项目任务(例如运行shell,运行单元测试,运行迁移)。

There's an example of the compose integration and running db migrations here: https://github.com/dnephin/dobi/tree/master/examples/init-db-with-rails 这里有一个组成集成和运行数据库迁移的示例: https : //github.com/dnephin/dobi/tree/master/examples/init-db-with-rails

Well why don't you use depends_on tag 好吧,为什么不使用depends_on标签

   depends_on: 
      - <YourDockerContainer>

Looks at this example below where 看下面这个例子

Kibana is dependant on ElasticSearch and fluentd, while Elasticsearch is dependant on fluentd using depends_on tag Kibana依赖于ElasticSearch和fluentd,而Elasticsearch依赖于使用depends_on标签的fluentd

 fluentd:
    image: fluentd

 elasticsearch:
    image: elasticsearch
 depends_on: 
   - fluentd  

 kibana:
    image: kibana
 depends_on: 
   - fluentd
  - elasticsearch

Order of Start will be 开始顺序为

1> fluentd 1>流利

2> elasticsearch 2> elasticsearch

3> kibana 3>基巴纳

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM