Eric J Ma's Website

Phony targets in Makefiles

written by Eric J. Ma on 2021-03-04 | tags: automation computation reproducibility


Makefiles can be used to create a lightweight CLI entry point for your project. By writing a Makefile, you can automate repetitive shell commands, such as building a Dockerfile (which might require you to remember certain incantations):

.PHONY: container

container:
    docker build -t mycontainer -f Dockerfile .

Doing so allows us to build a Docker container using the command make container when at the command line.

Now, you'll probably notice the .PHONY line at the top. What's that all about?

By convention, the command container would have referred to a file target on disk. (This is how Makefiles were originally designed.) However, if we want the command to be independent of any files on disk, we declare it to be a phony target that doesn't actually exist on disk.


I send out a newsletter with tips and tools for data scientists. Come check it out at Substack.

If you would like to sponsor the coffee that goes into making my posts, please consider GitHub Sponsors!

Finally, I do free 30-minute GenAI strategy calls for organizations who are seeking guidance on how to best leverage this technology. Consider booking a call on Calendly if you're interested!