FastAPI hello world with docker container.

This commit is contained in:
Tom Alexander
2024-10-15 19:16:25 -04:00
parent f70303193b
commit e1e31d0ca3
8 changed files with 491 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
FROM alpine:3.20 AS builder
RUN apk add --no-cache python3 poetry py3-poetry-plugin-export
RUN mkdir /source
WORKDIR /source
COPY pyproject.toml poetry.lock ./
RUN poetry export --output=requirements.txt
FROM alpine:3.20 AS runner
RUN addgroup web && adduser -D -G web web && install -d -D -o web -g web -m 700 /source
WORKDIR /source
RUN apk add --no-cache python3 py3-pip uvicorn
COPY --from=builder /source/requirements.txt /tmp
RUN pip install --break-system-packages --no-cache-dir --upgrade -r /tmp/requirements.txt && rm /tmp/requirements.txt
USER web
COPY --chown=web:web . .
ENTRYPOINT ["python", "-m", "uvicorn", "api_server.main:app", "--host", "0.0.0.0", "--port", "8080"]

View File

@@ -0,0 +1,32 @@
SHELL := bash
.ONESHELL:
.SHELLFLAGS := -eu -o pipefail -c
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
OS:=$(shell uname -s)
ifeq ($(origin .RECIPEPREFIX), undefined)
$(error This Make does not support .RECIPEPREFIX. Please use GNU Make 4.0 or later)
endif
.RECIPEPREFIX = >
IMAGE_NAME:= gateway-test
TARGET :=
.PHONY: help
help:
> @grep -h "##" $(MAKEFILE_LIST) | grep -v grep | sed -E 's/^([^:]*): *## */\1: /'
.PHONY: build
build: ## Build the docker image.
> docker build --tag $(IMAGE_NAME) --target=$(TARGET) --file Dockerfile ../
.PHONY: shell
shell: ## Launch an interactive shell inside the docker image.
shell: build
> docker run --rm -i -t --entrypoint /bin/sh --mount type=tmpfs,destination=/tmp $(IMAGE_NAME)
.PHONY: clean
clean:
> docker rmi $(IMAGE_NAME)

View File

@@ -0,0 +1,19 @@
name: apiserver
services:
api_server:
build:
context: ../
dockerfile: ./docker/Dockerfile
target: runner
container_name: api-server
ports:
- "8080:8080"
restart: always
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/_health"]
interval: 1m30s
timeout: 10s
retries: 3
start_period: 40s
start_interval: 5s