docker - How to docker-compose up only for services?
问题描述
In my docker-compose.yml
file I have plenty of services:
- nginx
- mysql
- redis
- echo
- php
- ...
But also some CLI utilities that I use with docker-compose run --rm
:
- artisan
- composer
- npm
- ...
When I start my system I do docker-compose up
. Unfortunately this also try to start all the CLI utilities. Is there a way to separate these two categories in my docker-compose?
解决方案
Update
Starting with docker-compose
1.28.0 the new service profiles are just made for that! With profiles
you can mark services to be only started in specific 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
:
# 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 the documentation. 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.
推荐阅读
- python - 将变量传递给表单操作
- python - 循环遍历多个 txt 文件并计算 Python 中所选单词的频率
- python - Python Dash - 来自另一个文件的回调值
- rest - Spring REST 应用程序中的存储库
- python - 如何随机获取 numpy 数组的一定数量的元素,每个类至少有一个元素?
- python - 在 Pandas 中有效地合并两个数据框的列和行
- kubernetes - Kubernetes HPA - 扩大冷却时间
- html - 不再有关于 HTTPS 的混合消息
- r - 如何根据分组条形图中特定变量的降序重新排序
- java - NoClassDefFoundError: io/restassured/RestAssured