If you manage multiple containerized projects spread across different folders, it can be inconvenient to manually run docker compose up
for each one. In this article, I’ll show you how to create a Bash script that automatically locates all docker-compose.yml
files and starts each corresponding project.
Objective
Scan a main directory (and all its subdirectories) for docker-compose.yml
or docker-compose.yaml
files, and run docker compose up -d
for each one.
Bash Script
#!/bin/bash
# Main directory where to search for projects
ROOT_DIR="$HOME/projects"
# Find all docker-compose.* files and start the containers
find "$ROOT_DIR" -type f \( -name "docker-compose.yml" -o -name "docker-compose.yaml" \) | while read -r compose_file; do
project_dir=$(dirname "$compose_file")
echo "Starting project in: $project_dir"
(
cd "$project_dir" && docker compose up -d
)
done
Usage Instructions
- Copy the above code into a file named, for example,
start_projects.sh
. - Make the file executable:
chmod +x start_projects.sh
- Run the script:
./start_projects.sh
Considerations
- The script uses
docker compose
, the new recommended syntax instead of the deprecateddocker-compose
command. - You can customize
ROOT_DIR
to match your system. - The
-d
flag starts the containers in the background. If you want to see logs, remove it. - To avoid entering unwanted directories (like
node_modules
), you can add a filter tofind
.
Possible Extensions
You can also adapt the script to:
- Stop all projects with
docker compose down
. - Check status with
docker compose ps
. - Log output to a file for any errors.
This approach allows you to efficiently manage multi-project Docker environments, automating startup and making your development setup more productive.