From 6f3de3a8853243d302132083edefe3f87f60dc07 Mon Sep 17 00:00:00 2001 From: Paul Harrison Date: Thu, 15 Dec 2022 16:43:09 +0000 Subject: [PATCH] feat: Dockerize API --- Dockerfile | 31 +++++++++++++++++++++++++++++++ Makefile | 8 ++++++-- README.md | 2 +- compose.yml | 9 +++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 Dockerfile create mode 100644 compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0497282 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,31 @@ +FROM python:3.11-slim AS os-cache + +ENV TZ=UTC +ENV PYTHONUNBUFFERED=1 +ENV POETRY_NO_INTERACTION=1 +ENV POETRY_VIRTUALENVS_CREATE=true +ENV POETRY_VIRTUALENVS_IN_PROJECT=true + +ARG DEBIAN_FRONTEND=noninteractive +RUN apt update -y +RUN apt upgrade -y +RUN apt install -y build-essential + +WORKDIR /app + +# FIXME: README.md seemed to be required here but I'm not sure why! +COPY poetry.lock pyproject.toml Makefile README.md ./ + +RUN pip install "poetry~=1.2" + +FROM os-cache AS image + +COPY poker ./poker +RUN make install-prod + +FROM os-cache AS test-image + +COPY setup.cfg ./ +COPY poker ./poker +COPY tests ./tests +RUN make install diff --git a/Makefile b/Makefile index 82bf93e..fc1c16d 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ SHELL := /bin/bash .PHONY: api api: ## Run API - @poetry run uvicorn poker.api:app + @poetry run uvicorn --host 0.0.0.0 poker.api:app .PHONY: black black: ## Run black formatter @@ -13,6 +13,10 @@ black: ## Run black formatter black-check: ## Run black formatter @poetry run black poker tests --check; +.PHONY: build +build: ## Build Docker container + @COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker compose -f compose.yml build; + .PHONY: clean clean: ## Remove python cache files -@find . -name '*.pyc' -exec rm -f {} +; @@ -79,4 +83,4 @@ uninstall: ## Remove virtual environment .PHONY: update update: ## Update poetry.lock using pyproject.toml - @poetry update; \ No newline at end of file + @poetry update; diff --git a/README.md b/README.md index 4eef272..cec2780 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ Result: "straight flush: 10-high diamonds" ## Usage - Install with `make install`. - Run linting and tests with `make quality test coverage clean`. -- Run API with `make API`. +- Build the API container with `make build`, then run API with `docker compose up -d`. - Check API health with `curl localhost:8000` - Query API for rank with e.g. ```shell diff --git a/compose.yml b/compose.yml new file mode 100644 index 0000000..fc4fbc6 --- /dev/null +++ b/compose.yml @@ -0,0 +1,9 @@ +services: + api: + container_name: poker-api + build: + context: . + dockerfile: Dockerfile + ports: + - "8000:8000" + command: make api