When working with a PHP/Symfony project, especially when using docker, it can be a little fiddly and time-consuming to connect to the container and execute the desired command. You can simplify this by creating a Makefile
and adding your repetitive commands, including which service to run them against.
Sample Usage
Clear the Symfony cache
make cache-clear
Update the composer vendor packages
make composer-update
Check that the database schema is valid
make validate
Makefile
.PHONY: help
help:
@echo 'Following targets exist:'
@echo ''
@echo ' cache-clear - clear the Symfony cache'
@echo ' composer-install - install the specified vendor packages'
@echo ' composer-update - update the vendor packages'
@echo ' validate - check the entities match the database schema'
@echo ''
.PHONY: cache-clear
cache-clear:
docker compose run --rm php bash -c "./bin/console cache:clear"
.PHONY: composer-install
composer-install:
docker compose run --rm php bash -c "composer install"
.PHONY: composer-update
composer-update:
docker compose run --rm php bash -c "composer update"
.PHONY: validate
validate:
docker compose run --rm php bash -c "./bin/console doctrine:schema:validate"
Setup
The above example assumes you are using docker compose
and have a compose.yaml
file configured with a service called php
defined. i.e.
services:
php:
image: php:latest
Notes
Tab Indentation
Makefile
uses tab indentation instead of spaces. If your Makefile
uses spaces you will see an error like
Makefile:9 *** missing separator. Stop.
Also, if you are copying the Makefile
from above to base yours from, you must replace the spaces with tabs.
PHP Flags
You can specify PHP flags in your make commands. For example, the PHP image I use for development has XDebug enabled but for some commands it is not relevant for XDebug to be running and you might want to turn it off i.e. so XDebug doesn't block a composer update or Symfony cache clear. You can achieve this by tweaking the Makefile
commands above like so
.PHONY: cache-clear
cache-clear:
docker compose run --rm php bash -c "XDEBUG_MODE=off ./bin/console cache:clear"
.PHONY: composer-install
composer-install:
docker compose run --rm php bash -c "XDEBUG_MODE=off composer install"
Similarly, if you needed a command to run against a different environment than the default one defined in .env
or .env.local
then you can do this via something like
.PHONY: cache-clear
cache-clear:
docker compose run --rm php bash -c "APP_ENV=test ./bin/console cache:clear"
or you can combine them
.PHONY: cache-clear
cache-clear:
docker compose run --rm php bash -c "XDEBUG_MODE=off APP_ENV=test ./bin/console cache:clear"