From 7ba0a4098767a69265e7b1ab7f8952599f11160c Mon Sep 17 00:00:00 2001 From: Pete Gadomski Date: Thu, 23 Feb 2023 13:42:50 -0800 Subject: [PATCH] Add backend-specific dockerfiles, publish images (#525) * ci: bump pgstac * ci, docker: add backend-specific dockerfiles * Add changelog entry --------- Co-authored-by: Nathan Zimmerman --- .github/workflows/cicd.yaml | 42 ++++++++++++++++++- CHANGES.md | 1 + README.md | 17 ++++++++ docker-compose.docs.yml | 2 +- docker-compose.yml | 2 +- Dockerfile => docker/Dockerfile | 0 Dockerfile.docs => docker/Dockerfile.docs | 0 docker/Dockerfile.pgstac | 25 ++++++++++++ docker/Dockerfile.sqlalchemy | 27 +++++++++++++ docker/docker-compose.pgstac.yml | 43 ++++++++++++++++++++ docker/docker-compose.sqlalchemy.yml | 49 +++++++++++++++++++++++ 11 files changed, 204 insertions(+), 4 deletions(-) rename Dockerfile => docker/Dockerfile (100%) rename Dockerfile.docs => docker/Dockerfile.docs (100%) create mode 100644 docker/Dockerfile.pgstac create mode 100644 docker/Dockerfile.sqlalchemy create mode 100644 docker/docker-compose.pgstac.yml create mode 100644 docker/docker-compose.sqlalchemy.yml diff --git a/.github/workflows/cicd.yaml b/.github/workflows/cicd.yaml index 95d85cfde..46aefd88e 100644 --- a/.github/workflows/cicd.yaml +++ b/.github/workflows/cicd.yaml @@ -15,7 +15,7 @@ jobs: services: db_service: - image: ghcr.io/stac-utils/pgstac:v0.6.12 + image: ghcr.io/stac-utils/pgstac:v0.6.13 env: POSTGRES_USER: username POSTGRES_PASSWORD: password @@ -130,7 +130,7 @@ jobs: backend: ["sqlalchemy", "pgstac"] services: pgstac: - image: ghcr.io/stac-utils/pgstac:v0.6.11 + image: ghcr.io/stac-utils/pgstac:v0.6.13 env: POSTGRES_USER: username POSTGRES_PASSWORD: password @@ -187,3 +187,41 @@ jobs: - uses: actions/checkout@v3 - name: Test generating docs run: make docs + + docker-build-push: + runs-on: ubuntu-latest + needs: [test, validate, test-docs] + permissions: + contents: read + packages: write + strategy: + fail-fast: true + matrix: + backend: ["sqlalchemy", "pgstac"] + steps: + - name: Checkout repository + uses: actions/checkout@v3 + - name: Log in to the Container registry + uses: docker/login-action@v2.1.0 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v4.3.0 + with: + images: ghcr.io/stac-utils/stac-fastapi + tags: | + type=schedule,suffix=-${{ matrix.backend }} + type=ref,event=branch,suffix=-${{ matrix.backend }} + type=ref,event=tag,suffix=-${{ matrix.backend }} + type=ref,event=pr,suffix=-${{ matrix.backend }} + - name: Build and push Docker image + uses: docker/build-push-action@v4.0.0 + with: + context: . + file: docker/Dockerfile.${{ matrix.backend }} + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/CHANGES.md b/CHANGES.md index 39d1f6e21..57ddeb2a6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,7 @@ * Validation checks in CI using [stac-api-validator](github.com/stac-utils/stac-api-validator) ([#508](https://github.com/stac-utils/stac-fastapi/pull/508)) * Required links to the sqlalchemy ItemCollection endpoint ([#508](https://github.com/stac-utils/stac-fastapi/pull/508)) +* Publication of docker images to GHCR ([#525](https://github.com/stac-utils/stac-fastapi/pull/525)) ### Changed diff --git a/README.md b/README.md index 4db19e73e..d26c12a32 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ packages: - **stac_fastapi.types**: Shared types and abstract base classes used by the library. #### Backends + - **stac_fastapi.sqlalchemy**: Postgres backend implementation with sqlalchemy. - **stac_fastapi.pgstac**: Postgres backend implementation with [PGStac](https://github.com/stac-utils/pgstac). @@ -61,9 +62,25 @@ pip install -e stac_fastapi/sqlalchemy pip install -e stac_fastapi/pgstac ``` +### Pre-built Docker images + +Pre-built images are available from the [Github Container Registry](https://github.com/stac-utils/stac-fastapi/pkgs/container/stac-fastapi). +The latest images are tagged with `latest-pgstac` and `latest-sqlalchemy`. +To pull the image to your local system: + +```shell +docker pull ghcr.io/stac-utils/stac-fastapi:latest-pgstac # or latest-sqlalchemy +``` + +This repository provides two example [Docker compose](https://docs.docker.com/compose/) files that demonstrate how you might link the pre-built images with a postgres/pgstac database: + +- [docker-compose.pgstac.yml](./docker/docker-compose.pgstac.yml) +- [docker-compose.sqlalchemy.yml](./docker/docker-compose.sqlalchemy.yml) + ## Local Development Use docker-compose via make to start the application, migrate the database, and ingest some example data: + ```bash make image make docker-run-all diff --git a/docker-compose.docs.yml b/docker-compose.docs.yml index 9c441f186..5ed87782e 100644 --- a/docker-compose.docs.yml +++ b/docker-compose.docs.yml @@ -5,7 +5,7 @@ services: container_name: stac-fastapi-docs-dev build: context: . - dockerfile: Dockerfile.docs + dockerfile: docker/Dockerfile.docs platform: linux/amd64 environment: - POSTGRES_USER=username diff --git a/docker-compose.yml b/docker-compose.yml index c3adb6592..506a4a37b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,7 +5,7 @@ services: image: stac-utils/stac-fastapi build: context: . - dockerfile: Dockerfile + dockerfile: docker/Dockerfile platform: linux/amd64 environment: - APP_HOST=0.0.0.0 diff --git a/Dockerfile b/docker/Dockerfile similarity index 100% rename from Dockerfile rename to docker/Dockerfile diff --git a/Dockerfile.docs b/docker/Dockerfile.docs similarity index 100% rename from Dockerfile.docs rename to docker/Dockerfile.docs diff --git a/docker/Dockerfile.pgstac b/docker/Dockerfile.pgstac new file mode 100644 index 000000000..1c1afe6f7 --- /dev/null +++ b/docker/Dockerfile.pgstac @@ -0,0 +1,25 @@ +FROM python:3.8-slim as builder + +RUN python -m venv /opt/venv + +ENV PATH="/opt/venv/bin:$PATH" + +WORKDIR /app + +COPY . /app + +RUN pip install ./stac_fastapi/types && \ + pip install ./stac_fastapi/api && \ + pip install ./stac_fastapi/extensions && \ + pip install ./stac_fastapi/pgstac[server] + + +FROM python:3.8-slim as pgstac + +ENV CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt + +COPY --from=builder /opt/venv /opt/venv + +ENV PATH="/opt/venv/bin:$PATH" + +CMD ["uvicorn", "stac_fastapi.pgstac.app:app", "--host", "0.0.0.0", "--port", "8080"] diff --git a/docker/Dockerfile.sqlalchemy b/docker/Dockerfile.sqlalchemy new file mode 100644 index 000000000..92b86b67e --- /dev/null +++ b/docker/Dockerfile.sqlalchemy @@ -0,0 +1,27 @@ +FROM python:3.8-slim as builder + +RUN python -m venv /opt/venv + +ENV PATH="/opt/venv/bin:$PATH" + +WORKDIR /app + +COPY . /app + +RUN pip install ./stac_fastapi/types && \ + pip install ./stac_fastapi/api && \ + pip install ./stac_fastapi/extensions && \ + pip install ./stac_fastapi/sqlalchemy[server] + + +FROM python:3.8-slim as sqlalchemy + +ENV CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt + +COPY --from=builder /opt/venv /opt/venv +COPY ./stac_fastapi/sqlalchemy/alembic /app/alembic +COPY ./stac_fastapi/sqlalchemy/alembic.ini /app/alembic.ini + +ENV PATH="/opt/venv/bin:$PATH" + +CMD ["uvicorn", "stac_fastapi.sqlalchemy.app:app", "--host", "0.0.0.0", "--port", "8080"] diff --git a/docker/docker-compose.pgstac.yml b/docker/docker-compose.pgstac.yml new file mode 100644 index 000000000..63514aab4 --- /dev/null +++ b/docker/docker-compose.pgstac.yml @@ -0,0 +1,43 @@ +version: '3' +services: + stac-fastapi-pgstac: + image: ghcr.io/stac-utils/stac-fastapi:latest-pgstac + platform: linux/amd64 + environment: + - APP_HOST=0.0.0.0 + - ENVIRONMENT=local + - POSTGRES_USER=username + - POSTGRES_PASS=password + - POSTGRES_DBNAME=postgis + - POSTGRES_HOST_READER=pgstac + - POSTGRES_HOST_WRITER=pgstac + - POSTGRES_PORT=5432 + - WEB_CONCURRENCY=10 + - VSI_CACHE=TRUE + - GDAL_HTTP_MERGE_CONSECUTIVE_RANGES=YES + - GDAL_DISABLE_READDIR_ON_OPEN=EMPTY_DIR + - DB_MIN_CONN_SIZE=1 + - DB_MAX_CONN_SIZE=1 + - USE_API_HYDRATE=${USE_API_HYDRATE:-false} + ports: + - "8080:8080" + depends_on: + - pgstac + + pgstac: + image: ghcr.io/stac-utils/pgstac:v0.6.13 + environment: + - POSTGRES_USER=username + - POSTGRES_PASSWORD=password + - POSTGRES_DB=postgis + - PGUSER=username + - PGPASSWORD=password + - PGHOST=localhost + - PGDATABASE=postgis + ports: + - "5439:5432" + command: postgres -N 500 + +networks: + default: + name: stac-fastapi-network diff --git a/docker/docker-compose.sqlalchemy.yml b/docker/docker-compose.sqlalchemy.yml new file mode 100644 index 000000000..70d216305 --- /dev/null +++ b/docker/docker-compose.sqlalchemy.yml @@ -0,0 +1,49 @@ +version: '3' +services: + stac-fastapi-sqlalchemy: + image: ghcr.io/stac-utils/stac-fastapi:latest-sqlalchemy + platform: linux/amd64 + environment: + - APP_HOST=0.0.0.0 + - APP_PORT=8080 + - POSTGRES_USER=username + - POSTGRES_PASS=password + - POSTGRES_DBNAME=postgis + - POSTGRES_HOST_READER=pgstac + - POSTGRES_HOST_WRITER=pgstac + - POSTGRES_PORT=5432 + - WEB_CONCURRENCY=10 + ports: + - "8080:8080" + depends_on: + - pgstac + + pgstac: + image: ghcr.io/stac-utils/pgstac:v0.6.13 + environment: + - POSTGRES_USER=username + - POSTGRES_PASSWORD=password + - POSTGRES_DB=postgis + - PGUSER=username + - PGPASSWORD=password + - PGHOST=localhost + - PGDATABASE=postgis + ports: + - "5439:5432" + command: postgres -N 500 + + migrate: + image: ghcr.io/stac-utils/stac-fastapi:latest-sqlalchemy + command: bash -c "cd /app && alembic upgrade head" + environment: + - POSTGRES_USER=username + - POSTGRES_PASS=password + - POSTGRES_DBNAME=postgis + - POSTGRES_HOST=pgstac + - POSTGRES_PORT=5432 + depends_on: + - stac-fastapi-sqlalchemy + +networks: + default: + name: stac-fastapi-network