Skip to content

Instantly share code, notes, and snippets.

@ngshiheng
Last active October 13, 2022 05:13
Show Gist options
  • Save ngshiheng/ef7b6ba71d9ad1b475994eb8afd703c9 to your computer and use it in GitHub Desktop.
Save ngshiheng/ef7b6ba71d9ad1b475994eb8afd703c9 to your computer and use it in GitHub Desktop.
.DEFAULT_GOAL := help
.PHONY: help
help:
@echo "Welcome to $(NAME)!"
@echo "Use 'make <target>' where <target> is one of:"
@echo ""
@echo " all run build -> stop -> run"
@echo " build build docker image based on shell ENV_VAR"
@echo " stop stop docker container"
@echo " run run docker container"
@echo ""
@echo "Go forth and make something great!"
all: build stop run
.PHONY: build
build:
@docker build -t image-name . --build-arg ENV_VAR=foo
.PHONY: stop
stop:
@docker stop container-name || true && docker rm container-name || true
.PHONY: run
run:
@docker run -d -e ANOTHER_VAR=bar--name container-name image-name
NAME := my-app
DOCKER := $(shell command -v docker 2> /dev/null)
ENV_VAR := $(shell echo $${ENV_VAR-development}) # NOTE: double $ for escaping
.PHONY: build
build: ## build docker image based on shell ENV_VAR
@if [ -z $(DOCKER) ]; then echo "docker is missing."; exit 2; fi # tip
@docker build -t $(NAME) . --build-arg ENV_VAR=$(ENV_VAR)
all: build stop run ## run build -> stop -> run
.PHONY: build
build: ## build docker image
@docker build -t image-name . --build-arg ENV_VAR=foo
.PHONY: stop
stop: ## stop running container
@docker stop container-name || true && docker rm container-name || true
.PHONY: run
run: ## run docker container
@docker run -d -e ANOTHER_VAR=bar--name container-name image-name
.DEFAULT_GOAL := help
.PHONY: help
help: ## display this help message
@awk 'BEGIN {FS = ":.*##"; printf "\\nUsage:\\n make \\033[36m<target>\\033[0m\\n\\nTargets:\\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \\033[36m%-10s\\033[0m %s\\n", $$1, $$2 }' $(MAKEFILE_LIST)
all: build stop run # build -> stop -> run
build:
@docker build -t image-name . --build-arg ENV_VAR=foo
stop:
@docker stop container-name || true && docker rm container-name || true
run:
@docker run -d -e ANOTHER_VAR=bar--name container-name image-name
# run `make <target>` to run this rule
<target>: <prerequisite 1> <prerequisite 2> <prerequisite N> # a comment block is prefixed with a "#"
<command 1>
<command 2>
<command N>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment