Simplify Running Docker Commands With Make

Photo by Ian Taylor on Unsplash

Simplify Running Docker Commands With Make

Typing and running complex docker commands in the terminal can sometimes feel like repetitive and tedious work. In this article, we will learn how to simplify running docker commands by using Make and Makefiles.

Pre-requisites

To follow along with this article, you need to have the following packages installed in your development environment:

  • Docker

  • Docker-compose

  • make

What is Make?

Make is a Unix utility tool that executes and compiles software programs. For Make to do its job, it reads instructions that are defined in a Makefile.

What is a Makefile?

A Makefile is a file that contains a set of rules or commands that are executed by the make utility tool.

Why use Make and Makefiles?

Make was primarily developed to compile software programs, but it's flexible enough to be used for other use cases. One of the use cases is the ability to wrap complex bash or shell commands into simple memorable actions (similar to functions in object-oriented programming languages). This saves you the hustle of having to remember and type long commands into the terminal every time you need to use them. This is especially useful if you work with tools like docker or docker-compose where you frequently have to run longer docker commands in the terminal.

Understanding Makefile syntax

Before we dive into how to use Makefiles, it is worth considering its syntax. Below is the structure of a Makefile.

target: dependencies
  commands

In the above syntax, target can be a name of a file, an executable or a name of action. In our case, the target will be the name of an action since we have no dependencies. When a target is not a name of a file and does not have dependencies it is referred to as a phony target.

dependencies are file objects that are taken as inputs to create the target. In our case, we do not need the dependencies.

commands are rules that can be executed by the shell. This is where we place our docker-related commands.

note that the command needs to be tab-delimited not space delimited

Docker-compose use case

Now that we know what Make and Makefile is and their structure, let's put them to use. In the following example, we will use the Make tool and Makefile to simplify running docker-compose commands.

Suppose we have two docker-compose files (docker-compose-dev.yml and docker-compose-prod.yml), one for the development environment and another for the production environment. For each environment, we have a set of docker commands we need to run. Let's explore how to use Make to run docker-compose commands for the two environments. For demonstration purposes, we will use the docker-compose up, docker-compose down and docker-compose logs commands. We won't be covering the contents of the docker-compose files as it is out of the scope of this article.

1. Create a Makefile

The first step is to create a Makefile. The Makefile should be created in the same directory where the docker-compose files are located.

2. Docker commands for the development environment

Let's add the docker commands for the development environment. We will use our docker-compose file for the development environment named docker-compose-dev.yml. Add the following to the created Makefile

start-dev:
  docker-compose -f docker-compose-dev.yml up

stop-dev:
  docker-compose -f docker-compose-dev.yml down

restart-dev:
  docker-compose -f docker-compose-dev.yml down && docker-compose -f docker-compose-dev.yml up

logs-dev:
  docker-compose logs

Now, to run the application or service in the development environment, run the following commands in the terminal:

make start-dev to start the application in a development environment.

make stop-dev to stop the application.

make restart-dev to restart the application.

make logs-dev to display docker logs.

3. Docker commands for the production environment

Finally, let's add the command for the production environment. In this case, we have a production docker-compose file named docker-compose-prod.yml. Below are the docker commands for the production environment.

start-prod:
  docker-compose -f docker-compose-prod.yml up

stop-prod:
  docker-compose -f docker-compose-prod.yml down

restart-prod:
  docker-compose -f docker-compose-prod.yml down && docker-compose -f docker-compose-prod.yml up

logs-prod:
  docker-compose logs

Use the following commands to run the application for the production environment:

make start-prod to start the application in production.

make stop-prod to stop the application in production.

make restart-prod to restart the application.

make logs-prod to show logs in production.

Conclusion

Make is not only limited to docker commands but can be used for other shell commands to simplify your workflow.

Additional Resources

Happy coding!!!