Makefile + Docker + Go: Why you should still use Makefile in modern application development

 

A lot of developers started recently in software engineering may have not even heard of the make command or maybe at least they used it to compile a c library but never thought about creating Makefile for there modern Go applications that lives in docker environments

To be fare make and Makefile are old technology developed in 1976[1] and may seem to be not very common today but it need your attention if you have not used it before

What do Makefile do?

Make is a build automation tool, wither you use it to build library, compile program, download dependencies or convert one file to different format, it is simply automation tool that have been battle tested for so long and stile valuable till today.

How Does it fit with Docker and Go?

Recently I have been working on multiple dockerized applications and for the latest one we have been using Go and where trying to automate build/test/publish our docker images and came the suggestion from anther team member to use Makefile my reaction was

Hmm, I have not used Makefile for a while now, I wander if it can help/fit within our modern environment

Lets see how Makefile works?

Makefile is simple it consist of (read more about it in the official documentations[2])

1- variables at the top of the file which you can use latter anywhere as $(VAR)

2- Target: which is the output file/folder of that step as in line #5 the target is folder named i18n-nodejs-3.0.0

3-Commands: List of commands that run to achieve the target

4- Pre-requests: as in line #9 after the : this is the name of another target that should run first before this one

Note if the target exists then it will not execute again so if you run make build where the folder i18n-nodejs-3.0.0 exists it will not download it again

This is a simple Makefile that download a tagged version of my i18n node.js module as zip from github and unzipe it, all of this in a target folder that named as in line #5 i18n-nodejs-$(REPO_TAG) which define the target from that step

Then cd into the directory and run npm install

all of that is automated throw the default target all which command make runs by default because it is the first target in the fire

How to use Makefile to automate Dockerized Go application build/test/publish?

First you need to think about the processes that you do on regular bases and need to automate, in our example this would be

1- building the Go binaries inside docker container

2- move the binary to the production container

3- publish the container

Now have a look on next two files Makefile & Dockerfile

You can see how simple the Makefile describing the jobs that it can do, first the default is the push new release of our application, which depend on building the docker image that use the Dockerfile from the same directory, which depend on creating the executable binary of our Go application

and now all what you need to release new version is type make in the terminal, and if we just want to build the executable we can type make build

as simple as this example looks you can imagine now what is the next level of downloading and extracting extra libraries that you need build the binary maybe have the build image published by itself and used in local to run the application.

Conclusion

That command that was created almost 50 years ago now still valuable and simple tool to automate parts of your application development life cycle, and maybe you should consider it in your next project too.

[1] https://en.wikipedia.org/wiki/Make_(software)

[2] https://www.gnu.org/software/make/manual/make.html

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.