cookiecutter-django
cookiecutter-django copied to clipboard
Makefile for use with docker
Description
I use Makefile with docker to make it simpler call the frequent actions used in daily django development.
I've added the most frequent one. Hope it helps.
Rationale
It's not really needed but i think it saves time and remembering large commands.
Use case(s) / visualization(s)
write "make build" to set up the docker container
write "make up" to start container
write "make manage COMMAND" to call to any django command
This is a great addition. Is there a particular reason why is only for local? I would also find this useful in production.
Maybe a better alternative would be fabric instead? Similar idea/interface as Make, but in Python, so easier to maintain IMO.
Would help if we want to enable more advanced things like #1133
You can grab some commands from here if you like:
PROJECT_NAME=your_project
export COMPOSE_FILE=local.yml
.PHONY: up down stop prune ps shell logs
default: up
## help : Print commands help.
help : Makefile
@sed -n 's/^##//p' $<
## up : Start up containers.
up:
@echo "Starting up containers for for $(PROJECT_NAME)..."
# @see:https://github.com/docker/compose/issues/6464
#docker-compose pull
docker-compose build
docker-compose up -d --remove-orphans
## build : Build python image.
build:
@echo "Building python image for for $(PROJECT_NAME)..."
docker-compose build
## down : Stop containers.
down: stop
## start : Start containers without updating.
start:
@echo "Starting containers for $(PROJECT_NAME) from where you left off..."
@docker-compose start
## stop : Stop containers.
stop:
@echo "Stopping containers for $(PROJECT_NAME)..."
@docker-compose stop
## prune : Remove containers and their volumes.
## You can optionally pass an argument with the service name to prune single container
## prune mariadb : Prune `mariadb` container and remove its volumes.
## prune mariadb solr : Prune `mariadb` and `solr` containers and remove their volumes.
prune:
@echo "Removing containers for $(PROJECT_NAME)..."
@docker-compose down -v $(filter-out $@,$(MAKECMDGOALS))
## ps : List running containers.
ps:
@docker ps --filter name='$(PROJECT_NAME)*'
## manage : Executes `manage.py` command.
## To use "--flag" arguments include them in quotation marks.
## For example: make manage "makemessages --locale=pl"
.PHONY: manage
manage:
docker-compose -f $(COMPOSE_FILE) exec django python manage.py $(filter-out $@,$(MAKECMDGOALS)) $(subst \,,$(MAKEFLAGS))
## shell : Access `python` container via shell.
shell:
docker exec -ti -e COLUMNS=$(shell tput cols) -e LINES=$(shell tput lines) $(shell docker ps --filter name='$(PROJECT_NAME)_django' --format "{{ .ID }}") bash
## logs : View containers logs.
## You can optionally pass an argument with the service name to limit logs
## logs python : View `python` container logs.
## logs nginx python : View `nginx` and `python` containers logs.
logs:
@docker-compose logs -f $(filter-out $@,$(MAKECMDGOALS))
# https://stackoverflow.com/a/6273809/1826109
%:
@:
Credits: https://github.com/wodby/docker4python
@browniebroke I've updated my code comment (manage action had a bug)
I personally like @pySilver offered option, it's a nice addition. Maybe we should open a new PR, except if the author can follow-up
@areski I'm using this for a long time and it is really helpful
This PR seems abandonded, but if someone can put together a new one based on enhancements, I'll look at it.
You can grab some commands from here if you like:
PROJECT_NAME=your_project export COMPOSE_FILE=local.yml .PHONY: up down stop prune ps shell logs default: up ## help : Print commands help. help : Makefile @sed -n 's/^##//p' $< ## up : Start up containers. up: @echo "Starting up containers for for $(PROJECT_NAME)..." # @see:https://github.com/docker/compose/issues/6464 #docker-compose pull docker-compose build docker-compose up -d --remove-orphans ## build : Build python image. build: @echo "Building python image for for $(PROJECT_NAME)..." docker-compose build ## down : Stop containers. down: stop ## start : Start containers without updating. start: @echo "Starting containers for $(PROJECT_NAME) from where you left off..." @docker-compose start ## stop : Stop containers. stop: @echo "Stopping containers for $(PROJECT_NAME)..." @docker-compose stop ## prune : Remove containers and their volumes. ## You can optionally pass an argument with the service name to prune single container ## prune mariadb : Prune `mariadb` container and remove its volumes. ## prune mariadb solr : Prune `mariadb` and `solr` containers and remove their volumes. prune: @echo "Removing containers for $(PROJECT_NAME)..." @docker-compose down -v $(filter-out $@,$(MAKECMDGOALS)) ## ps : List running containers. ps: @docker ps --filter name='$(PROJECT_NAME)*' ## manage : Executes `manage.py` command. ## To use "--flag" arguments include them in quotation marks. ## For example: make manage "makemessages --locale=pl" .PHONY: manage manage: docker-compose -f $(COMPOSE_FILE) exec django python manage.py $(filter-out $@,$(MAKECMDGOALS)) $(subst \,,$(MAKEFLAGS)) ## shell : Access `python` container via shell. shell: docker exec -ti -e COLUMNS=$(shell tput cols) -e LINES=$(shell tput lines) $(shell docker ps --filter name='$(PROJECT_NAME)_django' --format "{{ .ID }}") bash ## logs : View containers logs. ## You can optionally pass an argument with the service name to limit logs ## logs python : View `python` container logs. ## logs nginx python : View `nginx` and `python` containers logs. logs: @docker-compose logs -f $(filter-out $@,$(MAKECMDGOALS)) # https://stackoverflow.com/a/6273809/1826109 %: @:Credits: https://github.com/wodby/docker4python
i find this useful! thanks