Using a Makefile to shorten commands

Photo by Kara Eads on Unsplash

Using a Makefile to shorten commands

Foreword

You may already have known but I discovered that you can define a Makefile in which you can store your lengthy and commonly used commands. This feels like a step up from simple bash aliases. You could store a helpful Makefile into source control and share with your dev team.

Makefiles

Make sure you have make installed first in your machine. There are ton of tutorials online how to do so.

Let us see an example for creating shortcuts for our Docker commands. Like so:

up:
    docker-compose up -d

down:
    docker-compose down

shell:
    docker-compose exec web sh

Assuming I am in the same directory as the Makefile, I can use the command, make up to start my Docker services. Same goes with executing make down to shut them down and executing make shell to pop open a shell from my web container.

Closing Thoughts

Thanks to our Makefile, we no longer have to type lengthy commands. This article shed light more on the topic on why Makefiles are better than NPM scripts.