Can you define optional docker-compose services?

时光总嘲笑我的痴心妄想 提交于 2020-05-08 06:27:12

问题


Is there a way to define a Docker Compose service such that it is only brought up when you explicitly request it?

That is to say:

docker-compose up

would not start it, but

docker-compose up optional_service

would.


回答1:


One way to achieve that is to define your optional service in a different compose file. Then to start the optional service, run:

$ docker-compose -f docker-compose.yml -f optional-service.yaml up

For example, if I have a docker-compose.yml file that looks like:

version: '2.1'
services:
  lb:
    image: nginx:1.13           
  db:
    image: redis:3.2.9

I can extend it with an optional-service.yml that looks like:

version: '2.1'
services:
  busy:
    image: busybox

Notice that both compose files must use the same Compose file version.

You can read more about it in the Compose documentation.




回答2:


The extend keyword has been removed in version 3.x.

I faced the same issue and this is what I did:

  • From Docker's official docs, we can use a COMPOSE_FILE environment variable in a .env file.
  • The way this works is: when you do docker-compose up, instead of reading docker-compose.yml, it will read all the compose files specified in COMPOSE_FILE variable.

For example,

COMPOSE_FILE=docker-compose.yml:./optional/docker-compose.prod.yml
  • the good part is that:
    • the .env file is auto-read (if located in the same level as docker-compose.yml), when we invoke docker-compose up.
    • no need to specify each file with a -f flag in CLI.
  • The one drawback is: need to create separate compose files for each service you wish to make optional.

Note: separate file names with a : in case of linux or ; in case of windows.



来源:https://stackoverflow.com/questions/45680958/can-you-define-optional-docker-compose-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!