life is too short for a diary




Manage Your Dockerfiles Using Make

Tags: docker devops

Author
Written by: Tushar Sharma
Featured image for Manage Your Dockerfiles Using Make

The idea is to have a single entry point to start and stop all your services, without having to cd into each directory.

Project Structure

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.

The Master Makefile

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.

Postgres Service

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

Postgres Makefile

The postgres/Makefile is simple:

Postgres Docker Compose

Here is the content for postgres/docker-compose.yml:

Postgres Init Script

And the initialization script postgres/init-scripts/01-init-schema.sql:

Kafka Service

Similarly, for the kafka service:

mkdir kafka
cd kafka
touch Makefile docker-compose.yml

Kafka Makefile

The kafka/Makefile is identical to the Postgres one:

Kafka Docker Compose

And the kafka/docker-compose.yml:

Usage

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.


comments powered by Disqus