Skip to content
This repository was archived by the owner on Apr 2, 2025. It is now read-only.

Commit 6f8ccce

Browse files
committed
build: refactor to optimized docker multistage setup
1 parent 07b7804 commit 6f8ccce

File tree

6 files changed

+68
-19
lines changed

6 files changed

+68
-19
lines changed

.dockerignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1+
.git*
12
scripts
23
tests
3-
.git*

DOCKER.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Dockerized STAPI-FastAPI
2+
3+
The [Dockerfile](./Dockerfile) provides three targets:
4+
5+
- `base`: base install only, including poetry
6+
- `dev`: base plus dev dependencies
7+
- `lambda`: optimized for AWS Lambda, i.e. poetry removed
8+
9+
Both `dev` and `lambda` builds allow setting the backend class with the `BACKEND_NAME`
10+
env var, i.e. `BACKEND_NAME=stapi_fastapi_landsat:LandsatBackend`.
11+
12+
For local dev use, it's recommended to use the docker compose file. The backend can
13+
be choosen again by providing a env var `BACKEND_NAME`.

Dockerfile

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,49 @@
1-
FROM python:3.12-slim
1+
FROM python:3.12-slim AS base
22

3-
ENV PYTHONUNBUFFERED=1 \
3+
ENV PATH="/venv/bin:/opt/poetry/bin:$PATH" \
44
POETRY_VIRTUALENVS_CREATE=0 \
5-
VIRTUAL_ENV=/.venv \
6-
PATH="/.venv/bin:$PATH" \
7-
DEBIAN_FRONTEND=noninteractive
5+
PYTHONUNBUFFERED=1 \
6+
VIRTUAL_ENV=/venv
87

9-
RUN pip install --no-cache-dir --no-cache poetry
8+
RUN python3 -m venv /opt/poetry && \
9+
/opt/poetry/bin/pip3 install --no-cache-dir --no-cache poetry
1010

1111
WORKDIR /app
12-
1312
COPY poetry.lock pyproject.toml ./
13+
RUN --mount=type=cache,target=/root/.cache/pypoetry/cache \
14+
--mount=type=cache,target=/root/.cache/pypoetry/artifacts \
15+
python3 -m venv /venv && \
16+
poetry install --no-interaction --compile && \
17+
python3 -m compileall stapi_fastapi
18+
COPY . /app
1419

15-
RUN python3 -m venv /.venv && poetry install --no-cache --no-interaction --with lambda
16-
RUN apt-get -yq update && apt-get -yq install git && pip install \
17-
# git+https://github.com/stapi-utils/stat-fastapi-up42.git@main \
18-
git+https://github.com/stapi-utils/stat-fastapi-blacksky.git@main \
19-
git+https://github.com/stapi-utils/stat-fastapi-umbra.git@main \
20-
&& rm -rf /var/lib/apt/lists/*
2120

22-
COPY . /app
21+
FROM base AS dev
22+
23+
RUN --mount=type=cache,target=/root/.cache/pypoetry/cache \
24+
--mount=type=cache,target=/root/.cache/pypoetry/artifacts \
25+
poetry install --no-interaction --with dev --compile
26+
27+
ENV HOST=0.0.0.0
28+
29+
CMD [ "/opt/poetry/bin/poetry", "run", "dev" ]
30+
31+
FROM base AS lambda-builder
32+
33+
RUN --mount=type=cache,target=/root/.cache/pypoetry/cache \
34+
--mount=type=cache,target=/root/.cache/pypoetry/artifacts \
35+
poetry install --no-interaction --with lambda --compile
36+
37+
38+
FROM python:3.12-slim AS lambda
39+
40+
ENV PATH="/venv/bin:$PATH" \
41+
PYTHONUNBUFFERED=1 \
42+
VIRTUAL_ENV=/venv
43+
44+
WORKDIR /app
45+
COPY --from=lambda-builder /app /app
46+
COPY --from=lambda-builder /venv /venv
2347

24-
RUN poetry run python -c "import compileall; compileall.compile_path(maxlevels=10)" && poetry run python -m compileall stapi_fastapi
48+
ENTRYPOINT [ "python3", "-m", "awslambdaric" ]
49+
CMD ["lambda_handler.handler"]

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export DATABASE=sqlite:///order.sqlite
2323
export SPATIALITE_LIBRARY_PATH=/path/to/mod_spatialite.dylib
2424
```
2525

26+
Also see [DOCKER.md](./DOCKER.md) for details on docker setup for and deployment.
27+
2628
### Dev Setup
2729

2830
Setup is managed with `poetry` and `pre-commit`, all of which can be initialised
@@ -37,6 +39,7 @@ pytest flags are passed along
3739

3840
For dev purposes, [stapi_fastapi.**dev**.py](./stapi_fastapi/__dev__.py) shows
3941
a minimal demo with `uvicorn` to run the full app. Start it with `./scripts/server`.
42+
Choose backend with `BACKEND_NAME` env var, defaults to Landsat backend.
4043

4144
### Implementing a backend
4245

compose.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
services:
2+
api:
3+
build:
4+
context: .
5+
target: dev
6+
ports:
7+
- 8000:8000
8+
volumes:
9+
- .:/app
10+
environment:
11+
BACKEND_NAME: ${BACKEND_NAME:-stapi_fastapi_landsat:LandsatBackend}

deploy_cdk/app.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ def __init__(self, scope: Construct, id: str, profile: DeploymentProfile) -> Non
2828
code=_lambda.Code.from_ecr_image(
2929
repository=repository,
3030
tag_or_digest=profile.image_tag_or_digest,
31-
entrypoint=["poetry", "run", "python3", "-m", "awslambdaric"],
32-
cmd=["lambda_handler.handler"],
33-
working_directory="/app",
3431
),
3532
memory_size=profile.memory,
3633
timeout=Duration.seconds(profile.timeout),

0 commit comments

Comments
 (0)