cookiecutter-django icon indicating copy to clipboard operation
cookiecutter-django copied to clipboard

Makefile for use with docker

Open angoru opened this issue 6 years ago • 9 comments

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

angoru avatar Nov 29 '18 16:11 angoru

This is a great addition. Is there a particular reason why is only for local? I would also find this useful in production.

demestav avatar Mar 20 '19 14:03 demestav

Maybe a better alternative would be fabric instead? Similar idea/interface as Make, but in Python, so easier to maintain IMO.

browniebroke avatar Mar 20 '19 15:03 browniebroke

Would help if we want to enable more advanced things like #1133

browniebroke avatar Mar 20 '19 15:03 browniebroke

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

pySilver avatar Jul 30 '19 03:07 pySilver

@browniebroke I've updated my code comment (manage action had a bug)

pySilver avatar Apr 27 '20 00:04 pySilver

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 avatar Jan 29 '21 17:01 areski

@areski I'm using this for a long time and it is really helpful

pySilver avatar Jan 29 '21 17:01 pySilver

This PR seems abandonded, but if someone can put together a new one based on enhancements, I'll look at it.

browniebroke avatar Feb 23 '21 12:02 browniebroke

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

jugurtha114 avatar Apr 19 '23 04:04 jugurtha114