diff options
| author | kj_sh604 | 2026-05-07 00:29:50 -0400 |
|---|---|---|
| committer | kj_sh604 | 2026-05-07 00:29:50 -0400 |
| commit | 236f5658b8cf3646908de09dd3de91142be8e129 (patch) | |
| tree | 01c1992d8cf2ec6dcc23b9cc7c2da15b92d622be /Makefile | |
| parent | 8e3e9b6de0143970fb49bd619c61921115c4fd67 (diff) | |
refactor: container run commands
Diffstat (limited to 'Makefile')
| -rw-r--r-- | Makefile | 55 |
1 files changed, 53 insertions, 2 deletions
@@ -3,15 +3,20 @@ SHELL := /bin/sh IMAGE ?= kj-diagramming:latest BUILD_DIR := excalidraw-app/build PROJECT_MARKER := excalidraw-app/App.tsx +CONTAINER_NAME ?= kj-diagramming +HOST_PORT ?= 8000 +CONTAINER_PORT ?= 8000 CONTAINER_ENGINE := $(shell if command -v docker >/dev/null 2>&1; then printf '%s' docker; elif command -v podman >/dev/null 2>&1; then printf '%s' podman; fi) -.PHONY: help build clean container check-yarn check-container-engine check-build-dir +.PHONY: help build clean container serve stop check-yarn check-container-engine check-build-dir help: @echo "targets:" @echo " make build build static assets into $(BUILD_DIR)" @echo " make clean rm $(BUILD_DIR) (with checks)" @echo " make container build kj-diagramming image" + @echo " make serve run $(CONTAINER_NAME) in daemon mode" + @echo " make stop stop $(CONTAINER_NAME)" check-yarn: @if ! command -v yarn >/dev/null 2>&1; then \ @@ -59,4 +64,50 @@ clean: container: check-yarn check-container-engine clean build @echo "building image $(IMAGE)..." - @$(CONTAINER_ENGINE) build -f Dockerfile -t "$(IMAGE)" .
\ No newline at end of file + @$(CONTAINER_ENGINE) build -f Dockerfile -t "$(IMAGE)" . + +serve: check-container-engine + @set -eu; \ + engine="$(CONTAINER_ENGINE)"; \ + name="$(CONTAINER_NAME)"; \ + image="$(IMAGE)"; \ + host_port="$(HOST_PORT)"; \ + container_port="$(CONTAINER_PORT)"; \ + if ! $$engine image inspect "$$image" >/dev/null 2>&1; then \ + echo "error: image $$image was not found."; \ + echo "run: make container"; \ + exit 1; \ + fi; \ + if $$engine ps --format '{{.Names}}' | grep -Fxq "$$name"; then \ + echo "error: container $$name is already running."; \ + echo "run: make stop"; \ + exit 1; \ + fi; \ + if command -v ss >/dev/null 2>&1; then \ + while ss -ltn "sport = :$$host_port" 2>/dev/null | grep -q LISTEN; do \ + host_port=$$((host_port + 1)); \ + done; \ + elif command -v lsof >/dev/null 2>&1; then \ + while lsof -nP -iTCP:$$host_port -sTCP:LISTEN >/dev/null 2>&1; do \ + host_port=$$((host_port + 1)); \ + done; \ + elif command -v netstat >/dev/null 2>&1; then \ + while netstat -ltn 2>/dev/null | awk '{print $$4}' | grep -Eq "[:.]$$host_port$$"; do \ + host_port=$$((host_port + 1)); \ + done; \ + else \ + echo "warning: no port-check utility found (ss, lsof, netstat)."; \ + echo "trying host port $$host_port without pre-check."; \ + fi; \ + echo "starting $$name on http://localhost:$$host_port"; \ + $$engine run -d --rm --name "$$name" -p "$$host_port:$$container_port" "$$image" >/dev/null; \ + echo "started $$name (http://localhost:$$host_port)" + +stop: check-container-engine + @if $(CONTAINER_ENGINE) ps --format '{{.Names}}' | grep -Fxq "$(CONTAINER_NAME)"; then \ + echo "stopping $(CONTAINER_NAME)..."; \ + $(CONTAINER_ENGINE) stop "$(CONTAINER_NAME)" >/dev/null; \ + echo "stopped $(CONTAINER_NAME)."; \ + else \ + echo "container $(CONTAINER_NAME) is not running."; \ + fi
\ No newline at end of file |
