简体   繁体   English

如何在 docker-compose 中基于 RAILS 环境启动不同的服务

[英]How do I have start different services based on RAILS environment in docker-compose

I would like to start service-A, service-B, service-C if RAILS_ENV is development
I would like to start service-A, service-B, service-D if RAILS_ENV is staging
I would like to start service-A, service-B, service-E if RAILS_ENV is production

How should I override intelligently in docker-compose.yaml file.我应该如何在 docker-compose.yaml 文件中智能地覆盖。

service-C, service-D, service-E are not variants of the same service, but could be completely different containers as well

My suggestion would be to call docker-compose from a script.我的建议是从脚本中调用 docker-compose。

If your docker-compose.yaml looks like this:如果您的 docker-compose.yaml 看起来像这样:

service-A:
    # Your service definition

service-B:
    # Your service definition
    depends_on:
        - service-A

service-C:
    # Your service definition
    depends_on:
        - service-B

service-D:
    # Your service definition
    depends_on:
        - service-B

service-E:
    # Your service definition
    depends_on:
        - service-B

Then, instead of calling docker-compose up , call a script that performs the conditional logic by checking environment variable RAILS_ENV, like this:然后,不要调用docker-compose up ,而是调用一个通过检查环境变量 RAILS_ENV 来执行条件逻辑的脚本,如下所示:

./start-services.sh

Where the script definition looks like this:脚本定义如下所示:

#!/bin/bash
if [ $RAILS_ENV== "development" ]
  then
  docker-compose up service-C
elif [ $TESTVAR == "staging" ]
  then
  docker-compose up service-D
elif [ $TESTVAR == "production" ]
  then
  docker-compose up service-E
else
  exit 1
fi

Of course, this implementation is a but unix-dependent, but I think it can be generalized to any operating system.当然,这个实现是依赖于unix的,但我认为它可以推广到任何操作系统。

Docker-compose has overrides , you can have: Docker-compose 有覆盖,你可以有:

  • shared base config in docker-compose.yml with service-A and service-B docker-compose.yml中的共享基本配置与服务-A 和服务-B
  • service-C config in docker-compose.override.yml for development (docker compose should pick up this one by default) docker-compose.override.yml 中的 service-C 配置用于开发( docker-compose.override.yml compose 应该默认选择这个)
  • service-E config in docker-compose.production.yml , and run like docker-compose.production.yml中的 service-E 配置,然后像这样运行
    docker-compose -f docker-compose.yml -f docker-compose.production.yml up -d
  • similarly for service-E in docker-compose.staging.yml docker-compose.staging.yml中的 service-E 类似

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

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