根据条件启动Docker容器 [英] Start a docker container based on condition

查看:130
本文介绍了根据条件启动Docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题-

我有一个包含6个服务的docker-compose.yml。当我执行docker-compose up时,所有6个容器都已启动,但我需要2个容器才能开始其工作,并根据条件休息4个容器。

I have a docker-compose.yml with 6 services. When i execute docker-compose up, all 6 containers gets started but i need 2 containers to start its work initially and rest 4 containers based on conditions.

说明-

6种服务组合在一起(所有用户2种服务,2位用户4种服务):-

6 services in compose(2 services for all users & 4 services for 2 users):-

-2为所有用户提供的服务

-2 service for all users


  • 我正在第一个容器中安装需要登录的页面

  • I'm mounting login required pages in first container

我维护了第二个容器中登录帐户的数据库

I maintained database for login accounts in 2nd container

为每个用户提供-2服务

-2 services for each users


  • 基于用户,我从登录页面重定向到其他应用程序

  • 至此,我将他/她的应用程序安装在一个容器中,并将数据库安装在另一个容器中。

有没有办法,当我运行docker-compose up时-它应该仅启动所有用户共有的2个服务,并且基于用户登录名,我需要启动其他2个服务,其中他/她的应用程序会nted ???

推荐答案

仅基于用户执行此操作就需要Docker Compose之外的逻辑。能够配置。

Doing this based upon user alone would require logic outside of Docker Compose, but you may be able to configure .

您也许可以使用两个Docker Compose文件来完成类似的结果。

You might be able to use two Docker Compose files to accomplish similar results.


  • 为所有用户提供2个服务-docker-compose.yml

  • 为2个用户提供4个服务- docker-compose.admin.yml

docker-compose.admin.yml 会在原始撰写文件以及其他容器中进行 compliment (compliment)或 override 设置。您可以使用替代来更改包括发送到容器的ENV变量或卷在内的所有内容。

In docker-compose.admin.yml would compliment or override settings in the original compose file, as well as additional containers. You can use overrides to change anything including ENV vars or volumes sent to the containers.

运行 docker-compose up 时,将为所有用户启动这两项服务。

When running docker-compose up the two services for all users will start.

您可以通过运行

docker-compose -f docker-compose.yml -f docker-compose.admin.yml up



别名



.bashrc 中设置别名会使每天使用起来更加容易。

Aliases

Setting up an alias in your .bashrc would make this much easier to use on a daily basis.

# For regular users
alias comp="docker-compose"

# For two special users
alias comp="docker-compose -f docker-compose.yml -f docker-compose.admin.yml"

通过此设置,所有用户都可以使用 comp up comp down 来获取

With this setup all users can use comp up or comp down and get the appropriate set of containers.

查看Docker Docs中的详细信息:

See details in the Docker Docs:

扩展撰写服务

这篇关于根据条件启动Docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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