Skip to content

Instantly share code, notes, and snippets.

@AntonioCS
Last active December 17, 2020 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AntonioCS/c7aef859d0a0da2f059c02e106449d05 to your computer and use it in GitHub Desktop.
Save AntonioCS/c7aef859d0a0da2f059c02e106449d05 to your computer and use it in GitHub Desktop.
Self documenting makefile template for docker (php)
#Parameters file, comment out if not used (will be needed for Mysql logging)
include parameters.inc.dist
-include parameters.inc
.DEFAULT_GOAL := help
.PHONY: help
compose_file_path = ./docker-compose.yml
dc = docker-compose --file $(compose_file_path)
service_php = php-fpm
service_mysql = mysql
service_webserver =
#ensure this matches 100% to what you have on phpstorm
phpstorm_server_config_name = my-server-name
service_redis = redis
xdebug_remote_host=host.docker.internal
xdebug_remote_port=10005
xdebug_idekey=PHPSTORM
xdebug_remote_enable=1
xdebug_remote_mode=req
#This will override xdebug_remote_host and xdebug will always try to connect back (if there are issues with the host enable this)
xdebug_remote_connect_back=0
xdebug_remote_autostart=1
#Set with the value to have xdebug out output to a file (ex.: xdebug_remote_log="/tmp/xdebug.log")
xdebug_remote_log=""
php_debug_file=main.php
#https://stackoverflow.com/a/14061796/8715 to allow parameters passed to make (might not work in Mac)
#added filter to include both php-composer-re and php-composer-re-dev, emulating an if (target == php-composer-re OR target == php-composer-re-dev)
ifeq ($@, $(filter $@,php-composer-re php-composer-re-dev))
COMPOSER_RE_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
$(eval $(COMPOSER_RE_ARGS):;@:)
endif
setup: start
setup: php-composer-install
setup: ## Run this to setup everything up (should only run once)
@echo Finished setup
start: ## Start containers
$(dc) up -d
stop: ## Stop containers
$(dc) stop
restart: stop
restart: start
restart: ## Restart all containers
@echo Finished restart
start-no-d: ## Start containers not in detach mode
$(dc) up
logs: ## Show container logs
$(dc) logs -f -t
status: ## List containers
$(dc) ps
create-networks: ## Create networks for docker services
for net in ${networks} ; do \
docker network create $$net ; \
done || true
@echo All networks created
params: ## Create parameters.inc from parameters.inc.dist
ifeq (,$(wildcard parameters.inc))
@tail -n +5 ./parameters.inc.dist > ./parameters.inc
@echo File parameters.inc created
else
@echo File parameters.inc is already created
endif
rebuild: ## Rebuild containers
$(dc) build
php-bash: ## Open bash prompt to php docker container
$(dc) exec $(service_php) bash
php-extensions: ## List all loaded extensions with version numbers
$(dc) exec $(service_php) bash -c "php -r 'foreach (get_loaded_extensions() as \$$extension) echo \"\$$extension: \" . phpversion(\$$extension).PHP_EOL;'"
php-composer-install: ## Run composer install
$(dc) exec $(service_php) composer install
php-clear-cache: ## Clear cache (symfony)
$(dc) exec $(service_php) bin/console cache:clear
php-remove-cache: ## Remove cache by using rm -rf (symfony)
$(dc) exec $(service_php) rm -rf ./var/cache/*
php-tail-dev-log: ## Tail the dev log (symfony)
$(dc) exec $(service_php) tail -f ./var/logs/dev.log
php-make-vendor-writable: ## Make the vendor folder writable (useful for debugging)
$(dc) exec $(service_php) chmod 777 -R vendor/
php-composer-re: ## Composer require. Ex.: make php-composer-re <package>
echo $(dc) exec $(service_php) composer require $(COMPOSER_RE_ARGS)
#This target is required because if you put --dev make will interpret it as an option for itself
php-composer-re-dev: ## Same as php-composer-re but with --dev
echo $(dc) exec $(service_php) composer require --dev $(COMPOSER_RE_ARGS)
php-run-tests-unit: ## Run all unit tests (and dump output to file)
$(dc) exec $(service_php) sh -c "vendor/bin/phpunit --testsuite unit > phpunit_dump" || true
#more info https://www.jetbrains.com/help/phpstorm/2019.2/debugging-a-php-cli-script.html
php-run-debug: ## execute specified file in var php_debug_file and set PHP_IDE_CONFIG and configure xdebug
docker-compose exec $(service_php) sh -c "export PHP_IDE_CONFIG=\"serverName=${phpstorm_server_config_name}\" && \
php \
-dxdebug.remote_host=${xdebug_remote_host} \
-dxdebug.remote_port=${xdebug_remote_port} \
-dxdebug.idekey=${xdebug_idekey} \
-dxdebug.remote_enable=${xdebug_remote_enable} \
-dxdebug.remote_mode=${xdebug_remote_mode} \
-dxdebug.remote_autostart=${xdebug_remote_autostart} \
-dxdebug.remote_connect_back=${xdebug_remote_connect_back} \
-dxdebug.remote_log=${xdebug_remote_log} \
${php_debug_file}"
php-list-env: ## List environment variables of php container
$(dc) exec $(service_php) printenv
mysql-bash: ## Mysql bash
$(dc) exec $(service_mysql) sh
mysql-log-status: ## Show logging status
$(dc) exec $(service_mysql) mysql -u $(mysql_user) -p$(mysql_pass) -e "SHOW GLOBAL VARIABLES LIKE 'general_log';"
mysql-log-enable: ## Enable logging
$(dc) exec $(service_mysql) touch $(mysql_log_file)
$(dc) exec $(service_mysql) mysql -u $(mysql_user) -p$(mysql_pass) -e "SET global general_log_file='$(mysql_log_file)'; SET global log_output = 'file'; SET global general_log = on;"
mysql-log-disable: ## Disable logging
$(dc) exec $(service_mysql) mysql -u $(mysql_user) -p$(mysql_pass) -e "SET global general_log = off;"
mysql-log-tail: ## Tail logs
$(dc) exec $(service_mysql) tail -f $(mysql_log_file)
webserver-bash: ## Bash webserver container
$(dc) exec $(service_webserver) bash
webserver-start: ## Webserver start
$(dc) up $(service_webserver)
webserver-stop: ## Webserver stop
$(dc) stop $(service_webserver)
webserver-restart: webserver-stop
webserver-restart: webserver-start
webserver-restart: ## Webserver restart
@echo Web server restarted
redis-clear-cache: ## Clear redis data
$(dc) exec $(service_redis) redis-cli FLUSHALL
help:
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-40s\033[0m %s\n", $$1, $$2}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment