Makefile help target

| Comments

In many projects to run complex commands or group of commands I use Makefile. To document it I add a help target.

Here is an exemple for dump and restore commands for PostgreSQL in docker-compose under alias db.

DATE = $(shell date +%d%m%Y)
path ?= .

help: ## show this help
	@echo 'usage: make [target] ...'
	@echo ''
	@echo 'targets:'
	@egrep '^(.+)\:\ .*##\ (.+)' ${MAKEFILE_LIST} | sed 's/:.*##/#/' | column -t -c 2 -s '#'

dump: ## dump db, usage 'make dump path=/path/to/dumps'
	docker-compose exec --user postgres db pg_dumpall --clean | gzip > $(path)/db_$(DATE).sql.gz
restore: ## restore db from dump file, usage 'make restore dump=dump.sql.gz'
	docker-compose stop web
	gunzip -c $(dump) | docker exec -i --user postgres `docker-compose ps -q db` psql
	docker-compose start web

So make or make help outputs:

usage: make [target] ...

targets:
help         show this help
dump         dump db, usage `make dump path=/path/to/dumps`
restore      restore db from dump file, usage `make restore dump=dump.sql.gz`

Docker clean commands

| Comments

When you start to use Docker, at some point there will be a question - where is all my space. Most of it is occupied by containers, images and dangling volumes.

An easy way to clean is to use Docker garbage collection by Spotify and docker volume rm commands. I have an alias for this.

alias docker-gc='docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc -e GRACE_PERIOD_SECONDS=172800 spotify/docker-gc && docker volume rm $(docker volume ls -qf dangling=true)'

On mac sometimes it’s easier to remove all, for this you need to remove Docker.qcow2 in the ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux folder. You can find this path in the Docker -> Preferences -> Advanced dialog.

1/4 ยป