如何结合基础docker-compose.yml与指定目录配置文件运行docker-compose
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
-fflag is repeated for each file/directory group. Always put the basedocker-compose.ymlfirst—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 andother.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

