Skip to main content

Makefile – A Simple Way to Automate Your Commands

Have you ever found yourself typing the same long command again and again in a project, and thought…
"Life would be so much easier if I didn’t have to type this every single time"?

That’s exactly where a Makefile can help you.


What is a Makefile?

A Makefile is just a plain text file where you define shortcuts for your commands.
It tells the make command what to do when you type something like:

make up

Instead of writing the whole long command every time, you just run make with the shortcut you defined.


A Simple Example

Let’s say you’re working with Docker and the command to start your app looks like this:

docker-compose -f docker-compose.development.yml up

Typing this again and again is boring. Instead, create a file named Makefile in your project root and add this:

up: docker-compose -f docker-compose.development.yml up

Now, all you need to do is run:

make up

Boom! Much shorter, much cleaner.


Adding More Commands

You can add as many shortcuts as you want inside your Makefile. For example:

up: docker-compose -f docker-compose.development.yml up down: docker-compose -f docker-compose.development.yml down logs: docker-compose -f docker-compose.development.yml logs -f
  • make up → Starts your containers

  • make down → Stops your containers

  • make logs → Shows real-time logs

This way, you don’t need to remember or re-type all the Docker flags.


Why Use Makefile?

  • Saves time → No more copy-pasting long commands.

  • Consistency → Your teammates can use the same shortcuts.

  • Not just for Docker → Works with anything (Rails, Node.js, tests, deployment).

  • Cross-platform → Works on Linux, macOS, and Windows (with tools like WSL).


Going Beyond Basics

Using Variables

Instead of repeating the same file name, you can store it in a variable:

FILE = docker-compose.development.yml up: docker-compose -f $(FILE) up down: docker-compose -f $(FILE) down

Now if you switch to another compose file, you only need to change it in one place.


Phony Targets

Some commands don’t create files (like up or down). To avoid confusion, add this at the top:

.PHONY: up down logs

This tells Make these are just shortcuts, not actual files.


Real-World Uses

Makefiles are super flexible. Apart from Docker, you can use them to:

  • Run tests

    test: pytest
  • Build a Rails app

    migrate: bin/rails db:migrate
  • Deploy your project

    deploy: git push heroku main

Basically, if it’s a command you type often → put it in a Makefile.


Conclusion

A Makefile is one of those small tools that make a huge difference in your daily workflow.
It’s simple, flexible, and saves you from typing the same commands again and again.

Next time you catch yourself repeating a long command, stop and think:
"Wait… should this go in my Makefile?"

Chances are, the answer is yes. 🚀


Comments