Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaces poetry with uv for faster builds #42

Open
wants to merge 7 commits into
base: tesseract-python
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
*.egg-info/
node_modules/
__pycache__/

*.pyc
*.gcp.json
*.gcp.encoded

Expand Down
104 changes: 83 additions & 21 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,35 +1,97 @@
FROM python:3.10 as builder
FROM ubuntu:mantic as builder

RUN pip install setuptools wheel poetry==1.8.3
# To use python3.10, switch to ubuntu:jammy

ENV POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_IN_PROJECT=1 \
POETRY_VIRTUALENVS_CREATE=1 \
POETRY_CACHE_DIR=/tmp/poetry_cache
RUN <<EOT
apt-get update -qy
apt-get install -qyy \
-o APT::Install-Recommends=false \
-o APT::Install-Suggests=false \
build-essential \
ca-certificates \
python3-setuptools \
python3.12-dev
EOT

WORKDIR /app
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv

COPY pyproject.toml poetry.lock ./
RUN touch README.md
ENV UV_LINK_MODE=copy \
UV_COMPILE_BYTECODE=1 \
UV_PYTHON_DOWNLOADS=never \
UV_PYTHON=python3.12 \
UV_PROJECT_ENVIRONMENT=/app

RUN --mount=type=cache,target=$POETRY_CACHE_DIR poetry install --without dev --no-root
COPY pyproject.toml /_lock/
COPY uv.lock /_lock/

FROM python:3.10-slim-buster as runtime
RUN --mount=type=cache,target=/root/.cache <<EOT
cd /_lock
uv sync \
--frozen \
--no-dev \
--no-install-project
EOT

ENV VIRTUAL_ENV=/app/.venv \
PATH="/app/.venv/bin:$PATH"
COPY . /src
RUN --mount=type=cache,target=/root/.cache <<EOT
uv pip install \
--python=$UV_PROJECT_ENVIRONMENT \
--no-deps \
/src
EOT

WORKDIR /app
# ============================================================================ #

FROM ubuntu:mantic as runtime

# Include virtual environment in PATH
ENV PATH=/app/bin:$PATH

# Create the runtime user and group
RUN <<EOT
groupadd -r tesseract
useradd --system --home /app --gid tesseract --no-user-group tesseract
EOT

# create runtime user; install required dependencies
RUN useradd --system --uid 1001 tesseract &&\
chown -R tesseract:tesseract /app
ENTRYPOINT ["tini", "-v", "--", "/docker-entrypoint.sh"]

COPY --chown=tesseract --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}
# See <https://hynek.me/articles/docker-signals/>.
STOPSIGNAL SIGINT

COPY --chown=tesseract . /app
# Update OS packages, then clear APT cache and lists
RUN <<EOT
apt-get update -qy
apt-get install -qyy \
-o APT::Install-Recommends=false \
-o APT::Install-Suggests=false \
ca-certificates \
tini \
python3.12 \
libpython3.12 \
libpcre3 \
libxml2

# change user to tesseract user
apt-get clean
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
EOT

# Copy runtime files
COPY docker-entrypoint.sh /
COPY etc /app/etc

COPY --from=builder --chown=tesseract:tesseract /app /app
COPY --chown=tesseract:tesseract ./app.py /app/app.py

# Replace runtime user and cwd
USER tesseract
WORKDIR /app

CMD exec granian --interface asgi --host 0.0.0.0 --port 7777 --respawn-failed-workers app:layer
# Tests to ensure correct configuration and permissions
RUN <<EOT
python -V
python -Im site
python -Ic 'import server'
ls -l /docker-entrypoint.sh
ls -la /app
ls -l /app/etc
EOT
22 changes: 10 additions & 12 deletions app/__init__.py → app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@
from tesseract_olap import OlapServer
from tesseract_olap.logiclayer import TesseractModule

from .debug import DebugModule

from server.debug import DebugModule

# PARAMETERS ===================================================================

# These parameters are required and will prevent execution if not set
olap_backend = os.environ["TESSERACT_BACKEND"]
olap_schema = os.environ["TESSERACT_SCHEMA"]

# These parameters are optional
olap_cache = os.environ.get("TESSERACT_CACHE", ":memory:")
olap_schema = os.environ.get("TESSERACT_SCHEMA", "etc/schema")
olap_cache = os.environ.get("TESSERACT_CACHE", "")
app_debug = os.environ.get("TESSERACT_DEBUG", None)
log_filepath = os.environ.get("TESSERACT_LOGGING_CONFIG", "logging.ini")
commit_hash = os.environ.get("GIT_HASH", "")
log_filepath = os.environ.get("TESSERACT_LOGGING_CONFIG", "etc/logging.ini")

app_debug = bool(app_debug)

Expand All @@ -36,22 +34,22 @@


# ASGI app =====================================================================
olap = OlapServer(backend=olap_backend, schema=olap_schema)

mod_tsrc = TesseractModule(olap)
olap = OlapServer(backend=olap_backend, schema=olap_schema, cache=olap_cache)

mod_cmplx = EconomicComplexityModule(olap)
mod_tsrc = TesseractModule(olap, debug=app_debug)

mod_debug = DebugModule()
mod_cmplx = EconomicComplexityModule(olap, debug=app_debug)

layer = LogicLayer(debug=app_debug)

if app_debug:
mod_debug = DebugModule()
layer.add_module("/debug", mod_debug)

layer.add_module("/tesseract", mod_tsrc)
layer.add_module("/complexity", mod_cmplx)
layer.add_static("/ui", "./explorer/", html=True)
layer.add_static("/ui", "./etc/static/", html=True)


@layer.route("/", response_class=RedirectResponse, status_code=302)
def route_index():
Expand Down
22 changes: 0 additions & 22 deletions app/debug.py

This file was deleted.

2 changes: 1 addition & 1 deletion build-explorer/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const serverUrl = `/tesseract/`;
const config = {
base: "",
build: {
outDir: "../explorer/",
outDir: "../static/",
minify: "terser",
rollupOptions: {
output: {
Expand Down
Loading