Skip to content

Instantly share code, notes, and snippets.

@jnbdz
Last active April 10, 2023 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnbdz/af33b19ada7f533bfa91920142aa8fa8 to your computer and use it in GitHub Desktop.
Save jnbdz/af33b19ada7f533bfa91920142aa8fa8 to your computer and use it in GitHub Desktop.
Makefile for managing a single simple project running in a container.
include config.mk
.DEFAULT_GOAL := help
.PHONY: build
build: ## Build podman image
$(CONTAINER_ENGINE) build -t $(CONTAINER_NAME) .
.PHONY: run
run: open-ports ## Run the podman container
@trap 'exit 0' INT; \
($(CONTAINER_ENGINE) run --rm -it \
-p $(PORT) \
--net=host \
-v $(VOLUME) \
--name $(CONTAINER_NAME) \
$(CONTAINER_NAME) sh -c "yarn dev") &
wait
.PHONY: run-into
run-into: open-ports ## Run the podman container and go into it
$(CONTAINER_ENGINE) run --rm -it \
-p $(PORT) \
--net=host \
-v $(VOLUME) \
--name $(CONTAINER_NAME) \
$(CONTAINER_NAME) \
--entrypoint "/bin/bash"
.PHONY: stop
stop: ## Stop the podman container
$(CONTAINER_ENGINE) stop $(CONTAINER_NAME)
.PHONY: clean
clean: ## Clean the podman container
$(CONTAINER_ENGINE) rm -f $(CONTAINER_NAME)
.PHONY: exec
exec: ## Exec the podman container (to go in the container)
$(CONTAINER_ENGINE) exec --rm -it $(CONTAINER_NAME) bash
.PHONY: open-ports
open-ports:
@ports=$$(echo "$(PORT)" | awk -F'[ :]' '{for (i=1; i<=NF; i+=3) print $$i}') ; \
for port in $$ports; do \
echo "Opening port $$port" ; \
sudo ufw allow $$port/tcp ; \
done
.PHONY: help
help: ## Help
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sed 's/Makefile://' | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment