From 32a4af9dcfda9bfb3e547657feb3718a37ee0952 Mon Sep 17 00:00:00 2001 From: furyhawk Date: Sun, 31 May 2026 23:32:03 +0800 Subject: [PATCH] Add Docker and Makefile for containerized workflow --- .dockerignore | 17 +++++++++++++++ Dockerfile | 19 +++++++++++++++++ Makefile | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 38 ++++++++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 Makefile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..6d1df47 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,17 @@ +.git +.github +.venv +__pycache__ +*.pyc +.pytest_cache +.ruff_cache +.mypy_cache +.coverage +.env +.env.* +config/api_keys.json +tests +htmlcov +build +dist +*.egg-info diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..856180b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM python:3.13-slim + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 + +WORKDIR /app + +RUN pip install --no-cache-dir uv + +COPY pyproject.toml uv.lock README.md ./ +RUN uv sync --frozen --no-dev + +COPY src ./src +COPY config ./config +COPY openapi_json ./openapi_json + +EXPOSE 8000 + +CMD ["uv", "run", "uvicorn", "gateway_framework.main:app", "--host", "0.0.0.0", "--port", "8000", "--app-dir", "src"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..02a1907 --- /dev/null +++ b/Makefile @@ -0,0 +1,57 @@ +SHELL := /bin/sh + +CONTAINER_ENGINE ?= $(shell command -v podman >/dev/null 2>&1 && echo podman || echo docker) +IMAGE_NAME ?= openapi-api-gateway +IMAGE_TAG ?= latest +CONTAINER_NAME ?= openapi-api-gateway +PORT ?= 8000 +ENV_FILE ?= .env + +RUN_ENV := $(if $(wildcard $(ENV_FILE)),--env-file $(ENV_FILE),) + +.PHONY: help build run stop logs shell test test-container fmt clean + +help: + @echo "Targets:" + @echo " make build Build container image" + @echo " make run Run gateway container" + @echo " make stop Stop and remove container" + @echo " make logs Follow container logs" + @echo " make shell Open shell in running container" + @echo " make test Run local pytest suite with uv" + @echo " make test-container Run tests in container image" + @echo " make fmt Run ruff format checks" + @echo " make clean Remove image and stopped container" + +build: + $(CONTAINER_ENGINE) build -t $(IMAGE_NAME):$(IMAGE_TAG) -f Dockerfile . + +run: + $(MAKE) stop >/dev/null 2>&1 || true + $(CONTAINER_ENGINE) run -d \ + --name $(CONTAINER_NAME) \ + -p $(PORT):8000 \ + $(RUN_ENV) \ + $(IMAGE_NAME):$(IMAGE_TAG) + @echo "Gateway is running on http://127.0.0.1:$(PORT)" + +stop: + -$(CONTAINER_ENGINE) rm -f $(CONTAINER_NAME) + +logs: + $(CONTAINER_ENGINE) logs -f $(CONTAINER_NAME) + +shell: + $(CONTAINER_ENGINE) exec -it $(CONTAINER_NAME) sh + +test: + uv run pytest -q + +test-container: + $(CONTAINER_ENGINE) run --rm $(IMAGE_NAME):$(IMAGE_TAG) uv run pytest -q + +fmt: + uv run ruff check . + +clean: stop + -$(CONTAINER_ENGINE) rmi $(IMAGE_NAME):$(IMAGE_TAG) diff --git a/README.md b/README.md index 3760561..c964e53 100644 --- a/README.md +++ b/README.md @@ -114,3 +114,41 @@ Optional admin auth: - Run lint: `uv run ruff check .` - Add dependency: `uv add ` - Add dev dependency: `uv add --dev ` + +## Container (Docker or Podman) + Makefile +The repository includes a containerized workflow via `Dockerfile` and `Makefile`. + +### Prerequisites +- Install either Docker or Podman. +- By default, Makefile uses Podman when available; otherwise Docker. + +### Build Image +```bash +make build +``` + +### Run Container +```bash +make run +``` + +Gateway will be available at `http://127.0.0.1:8000`. + +### Stop and Inspect +```bash +make logs +make stop +``` + +### Useful Variables +- `CONTAINER_ENGINE=docker` (or `podman`) +- `PORT=8080` +- `IMAGE_NAME=openapi-api-gateway` +- `IMAGE_TAG=latest` +- `ENV_FILE=.env` + +Example: +```bash +make build CONTAINER_ENGINE=docker IMAGE_TAG=dev +make run CONTAINER_ENGINE=docker PORT=8080 ENV_FILE=.env +```