简体   繁体   English

docker-compose 如何仅用于服务?

[英]How to docker-compose up only for services?

In my docker-compose.yml file I have plenty of services:在我的docker-compose.yml文件中,我有很多服务:

  • nginx nginx
  • mysql mysql
  • redis redis
  • echo回声
  • php php
  • ... ...

But also some CLI utilities that I use with docker-compose run --rm :还有一些我与docker-compose run --rm

  • artisan工匠
  • composer作曲家
  • npm npm
  • ... ...

When I start my system I do docker-compose up .当我启动我的系统时,我执行docker-compose up Unfortunately this also try to start all the CLI utilities.不幸的是,这也会尝试启动所有 CLI 实用程序。 Is there a way to separate these two categories in my docker-compose?有没有办法在我的 docker-compose 中区分这两个类别?

Update更新

Starting with docker-compose 1.28.0 the new service profiles are just made for that!docker-compose 1.28.0开始,新的服务配置文件就是为此而生的! With profiles you can mark services to be only started in specific profiles:使用profiles ,您可以将服务标记为仅在特定配置文件中启动:

services:
  nginx:
    # ...
  mysql:
    # ...
  composer:
    profiles: ["cli-only"]
    # ...
    # ...
  npm:
    profiles: ["cli-only"]
    # ...
docker-compose up # start main services, no composer and no npm
docker-compose run --rm composer
docker-compose run --rm npm

original answer原始答案

Unfortunately there is currently no convenient way to do this.不幸的是,目前没有方便的方法来做到这一点。 The officially recommended way to do it is to separate your commands into its own docker-compose.yml :官方推荐的方法是将你的命令分成自己的docker-compose.yml

# start all your services
docker-compose up

# execute a defined command in docker-compose.cli.yml
docker-compose -f docker-compose.cli.yml run npm update

# if your command depends_on a service you need to merge the configs
docker-compose -f docker-compose.yml -f docker-compose.cli.yml run npm update

Specifying multiple docker-compose.yml files with the -f flag will merge them together, see thedocumentation .使用-f标志指定多个docker-compose.yml文件会将它们合并在一起,请参阅文档 This allows you to depend on services/networks/volumes which are defined in another file.这允许您依赖在另一个文件中定义的服务/网络/卷。

For an in-depth discussion on the whole issue see docker/compose#1896 .有关整个问题的深入讨论,请参见docker/compose#1896

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

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