## # Makefile to help manage docker-compose services # # Include environment files include .env export # Variables THIS_FILE := $(lastword $(MAKEFILE_LIST)) DOCKER := $(shell which docker) DOCKER_COMPOSE := $(shell which docker-compose) IMAGE_DEFAULT := wrappdsh CONTAINER_DEFAULT := wrappdsh SHELL_CMD := /bin/bash # Docker compose files DOCKER_COMPOSE_FILES := -f docker-compose.yml ifeq ($(DOCKER_ENV),local) DOCKER_COMPOSE_FILES := -f docker-compose.yml endif ifeq ($(DOCKER_ENV),production) DOCKER_COMPOSE_FILES := -f docker-compose.yml endif # Services SERVICES_DEFAULT := web ifeq ($(DOCKER_ENV),local) SERVICES_DEFAULT := web endif ifeq ($(DOCKER_ENV),production) SERVICES_DEFAULT := web endif SERVICE_DEFAULT := web container ?= $(CONTAINER_DEFAULT) image ?= $(IMAGE_DEFAULT) service ?= services ?= $(SERVICES_DEFAULT) .DEFAULT_GOAL := help ## # help # help: ifeq ($(CONTAINER_DEFAULT),) $(warning WARNING: CONTAINER_DEFAULT is not set. Please edit makefile) endif @echo @echo "Make targets:" @echo @cat $(THIS_FILE) | \ sed -n -E 's/^([^.][^: ]+)\s*:(([^=#]*##\s*(.*[^[:space:]])\s*)|[^=].*)$$/ \1 \4/p' | \ sort -u | \ expand -t15 @echo @echo @echo "Target arguments:" @echo @echo " " "service" "\t" "Target service for docker-compose actions (default=all-services)" @echo " " " " "\t" " - make start" @echo " " " " "\t" " - make start service=app" @echo " " "services" "\t" "Target services for docker-compose actions (default=all-services, space separated)" @echo " " " " "\t" " - make stop services='app db'" @echo " " "container""\t" "Target container for docker actions (default='$(CONTAINER_DEFAULT)')" @echo " " " " "\t" " - make bash container=$(container)" @echo " " "image" "\t" "Target image for interactive shell (default='$(IMAGE_DEFAULT)')" @echo " " " " "\t" " - make it image=$(image)" ## # services # services: ## Lists services @$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) ps --services ## # start # all: dev ## See 'dev' start: dev ## See 'dev' dev: ## Start containers for development [service|services] @$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) up -d $(services) $(MAKE) logs ## # stop # stop: ## Stop containers [service|services] @$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) stop $(services) ## # restart # restart: ## Restart containers [service|services] @$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) restart $(services) ## # down # down: ## Removes containers (preserves images and volumes) @$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) down ## # build # build: ## Builds service images [service|services] @$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) build --build-arg DOCKER_ENV=${DOCKER_ENV} $(services) ## # rebuild # rebuild: ## Build containers without cache [service|services] @$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) build --build-arg DOCKER_ENV=${DOCKER_ENV} --no-cache $(services) ## # ps # status: ps ## See 'ps' ps: ## Show status of containers [service|services] @$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) ps $(services) ## # interact # interact: it ## See `it' it: ## Run a new container in interactive mode. Needs image name [image] ifeq ($(image),) $(error ERROR: 'image' is not set. Please provide 'image=' argument or edit makefile and set CONTAINER_DEFAULT) endif @echo @echo "Starting interactive shell ($(SHELL_CMD)) in image container '$(image)'" @$(DOCKER) run -it --entrypoint "$(SHELL_CMD)" $(image) ## # bash # sh: bash ## See 'bash' shell: bash ## See 'bash' bash: ## Brings up a shell in default (or specified) container [container] ifeq ($(container),) $(error ERROR: 'container' is not set. Please provide 'container=' argument or edit makefile and set CONTAINER_DEFAULT) endif @echo @echo "Starting shell ($(SHELL_CMD)) in container '$(container)'" @$(DOCKER) exec -it "$(container)" "$(SHELL_CMD)" ## # attach # at: attach ## See 'attach' attach: ## Attach to a running container [container] ifeq ($(container),) $(error ERROR: 'container' is not set. Please provide 'container=' argument or edit makefile and set CONTAINER_DEFAULT) endif @echo @echo "Attaching to '$(container)'" @$(DOCKER) attach $(container) ## # log # log: ## Shows log from a specific container (in 'follow' mode) [container] ifeq ($(container),) $(error ERROR: 'container' is not set. Please provide 'container=' argument or edit makefile and set CONTAINER_DEFAULT) endif @echo @echo "Log in $(container)" @$(DOCKER) logs -f $(container) ## # logs # logs: ## Shows output of running containers (in 'follow' mode) [service|services] @echo @echo "Logs in $(services)" @$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) logs -f $(services) ## # rmimages # rmimages: ## Remove images @echo @echo "Remove local images" @$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) down --rmi local ## # clean # clean: ## Remove containers, images and volumes @echo @echo "Remove containers, images and volumes" @$(DOCKER_COMPOSE) $(DOCKER_COMPOSE_FILES) down --volumes --rmi all %: @: .PHONY: %