You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何结合基础docker-compose.yml与指定目录配置文件运行docker-compose

How to Combine Base Docker Compose File with All YAMLs in Specific Directories

Great question! Docker Compose lets you specify multiple configuration files using the -f flag, which is perfect for your use case where you want to merge the base docker-compose.yml with all YAML files in targeted directories. Here's how to make it work:

1. Use Shell Wildcards for Quick Setup

Your shell (like Bash or Zsh) can expand wildcards to automatically include all .yml files in a directory. From your parentdir, run this command:

docker-compose -f docker-compose.yml -f folder1/*.yml -f folder2/*.yml -f other.yml up
  • The -f flag is repeated for each file/directory group. Always put the base docker-compose.yml first—Docker Compose merges configurations in order, so later files will override conflicting settings from earlier ones.
  • This command will automatically pull in folder1/foo.yml, folder1/bar.yml, folder2/foo.yml, plus your base file and other.yml.

2. Create a Reusable Shell Script

If you don’t want to type the long command every time, save it to a shell script (e.g., start-compose.sh):

#!/bin/bash
docker-compose -f docker-compose.yml \
  -f folder1/*.yml \
  -f folder2/*.yml \
  -f other.yml \
  up -d  # Add -d to run containers in detached background mode

Make the script executable:

chmod +x start-compose.sh

Then start your services with a simple:

./start-compose.sh

3. Optional: Exclude Specific Files

If there are YAML files in the directories you want to skip, use extended glob patterns (enable this first in Bash with shopt -s extglob). For example, to exclude folder1/ignore-me.yml:

shopt -s extglob
docker-compose -f docker-compose.yml -f folder1/!(*ignore-me*.yml) -f folder2/*.yml -f other.yml up

内容的提问来源于stack exchange,提问作者cclloyd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:40:54