The idea is to have a single entry point to start and stop all your services, without having to cd into each directory.
Let's organize our project like this:
masterImages/
├── Makefile
├── postgres/
│ ├── docker-compose.yml
│ ├── init-scripts/
│ │ └── 01-init-schema.sql
│ └── Makefile
└── kafka/
├── docker-compose.yml
└── Makefile
The masterImages directory will contain our master Makefile and subdirectories for each service.
First, create the masterImages directory and the main Makefile within it.
mkdir masterImages
cd masterImages
touch Makefile
Now, add the following content to the masterImages/Makefile:
This master Makefile is the heart of our setup. It redirects make commands to the Makefile inside the respective service directory. For instance, make postgres start will execute the start target in the postgres/Makefile.
Now, let's set up the postgres service.
mkdir -p postgres/init-scripts
cd postgres
touch Makefile docker-compose.yml init-scripts/01-init-schema.sql
The postgres/Makefile is simple:
Here is the content for postgres/docker-compose.yml:
And the initialization script postgres/init-scripts/01-init-schema.sql:
Similarly, for the kafka service:
mkdir kafka
cd kafka
touch Makefile docker-compose.yml
The kafka/Makefile is identical to the Postgres one:
And the kafka/docker-compose.yml:
Now, from the masterImages directory, you can manage your services like this:
To start the Postgres service:
make postgres start
To stop the Postgres service:
make postgres stop
To start the Kafka cluster:
make kafka start
And to stop it:
make kafka stop
This approach keeps your projects organized and makes them easy to manage from a single place.