feat: Dockerize API

This commit is contained in:
Paul Harrison 2022-12-15 16:43:09 +00:00
parent 484c636513
commit c114366357
4 changed files with 47 additions and 3 deletions

31
Dockerfile Normal file
View File

@ -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

View File

@ -3,7 +3,7 @@ SHELL := /bin/bash
.PHONY: api .PHONY: api
api: ## Run API api: ## Run API
@poetry run uvicorn poker.api:app @poetry run uvicorn --host 0.0.0.0 poker.api:app
.PHONY: black .PHONY: black
black: ## Run black formatter black: ## Run black formatter
@ -13,6 +13,10 @@ black: ## Run black formatter
black-check: ## Run black formatter black-check: ## Run black formatter
@poetry run black poker tests --check; @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 .PHONY: clean
clean: ## Remove python cache files clean: ## Remove python cache files
-@find . -name '*.pyc' -exec rm -f {} +; -@find . -name '*.pyc' -exec rm -f {} +;

View File

@ -97,7 +97,7 @@ Result: "straight flush: 10-high diamonds"
## Usage ## Usage
- Install with `make install`. - Install with `make install`.
- Run linting and tests with `make quality test coverage clean`. - 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` - Check API health with `curl localhost:8000`
- Query API for rank with e.g. - Query API for rank with e.g.
```shell ```shell

9
compose.yml Normal file
View File

@ -0,0 +1,9 @@
services:
api:
container_name: poker-api
build:
context: .
dockerfile: Dockerfile
ports:
- "8000:8000"
command: make api