Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makefile for use with docker #1879

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

angoru
Copy link

@angoru angoru commented Nov 29, 2018

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

Copy link
Member

@browniebroke browniebroke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this contribution! I'd be in favour of seeing this. However, I have a few comments:

Finally, feel free to add yourself to the list of contributors 🎉

@docker-compose -f local.yml up

build:
@docker-compose -f local.yml up --force-recreate --build
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find a bit confusing that make build actually does a docker-compose up .... One might want to re-build and then run a manage command rather than starting the whole stack.

What's the reason for that? I'd rather keep them a bit more separate and only do docker-compose build.

We can chain commands easily with make: make build up,

Finally, could we accept an optional parameter to rebuild only one container, e.g. django?

@docker-compose -f local.yml up --force-recreate --build

manage:
@docker-compose -f local.yml run --rm django python ./manage.py $(filter-out $@,$(MAKECMDGOALS)) --settings=config.settings.local
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the --settings=config.settings.local? It's the default value, so we should be able to remove it.

Copy link

@elyak123 elyak123 Mar 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use a Makefile for docker too, and IMHO its quite useful because you can easily change from local to production;

buildev:
	@sed -i.bak s/DJANGO_SETTINGS_MODULE=config.settings.production/DJANGO_SETTINGS_MODULE=config.settings.local/g .env
	@sed -i.bak s/DJANGO_DEBUG=False/DJANGO_DEBUG=True/g .env
	@docker-compose -f docker-compose-dev.yml up --build

and later on:

buildprod:
	@sed -i.bak s/DJANGO_SETTINGS_MODULE=config.settings.local/DJANGO_SETTINGS_MODULE=config.settings.production/g .env
	@sed -i.bak s/DJANGO_DEBUG=True/DJANGO_DEBUG=False/g .env
	@docker-compose -f docker-compose.yml up --build

this way its idempotent. It doesnt matter if you are already on dev environment, it will work as expected and it will change seamlessly to production in one command.

@demestav
Copy link
Contributor

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

@browniebroke
Copy link
Member

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

@browniebroke
Copy link
Member

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

@pySilver
Copy link

pySilver commented Jul 30, 2019

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
Copy link

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

@areski
Copy link
Contributor

areski commented Jan 29, 2021

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

@pySilver
Copy link

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

@browniebroke
Copy link
Member

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

@jugurtha114
Copy link

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

@foarsitter
Copy link
Collaborator

Was looking into just and found out it can be installed through pypi. Has make that capability? If that ins't the case I would favor just over make.

@browniebroke
Copy link
Member

Was looking into just and found out it can be installed through pypi. Has make that capability? If that ins't the case I would favor just over make.

Yes I agree. Another benefit would be to have a more cross-platform tool, I don't think that make is widely available on Windows...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants