How to Pass Environment Variables to Makefiles in Subdirectories
We all know how to access environment variables inside our
Suppose this is our project structure.
📂 project ┣ 📜 Makefile ┣ 📜 .env ┗ 📂 client ┣ 📜 Dockerfile ┗ 📜 Makefile
/Makefile, suppose we trigger a
make command in the
# /Makefile build-client: cd client && $(MAKE) build
# /client/Makefile build: docker build -t client .
But let’s say
/client/Makefile needs access to some environment variables inside
# .env HOSTNAME=localhost PORT=3000
How can we pass the environment variables into the nested
In this scenario, we will need to
export the variable in the same command as
From the GNU documentation:
makecome from the environment in which
makeis run. Every environment variable that
makesees when it starts up is transformed into a
makevariable with the same name and value.
# /Makefile include .env build-client: export URI=$(HOSTNAME):$(PORT) && \ cd client && $(MAKE) build
\ will allow for a multi-line shell command.
URI variable exists in the environment of the
# /client/Makefile build: docker build -t client --build-arg URI=$(URI) .