File size: 4,513 Bytes
e6be672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e19c339
 
 
 
 
 
 
 
 
e6be672
e19c339
e6be672
 
 
 
 
e19c339
 
e6be672
 
 
 
 
 
 
e19c339
e6be672
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# BIM Query API for Hugging Face Spaces (Docker SDK).
# HF Spaces run as user 1000, expose a single port (7860), and have no
# persistent disk — everything writeable lives under /tmp or /app and is
# considered ephemeral.
#
# This deployment runs Postgres + pgvector INSIDE the container (Mattia's
# choice — see memory). The DB is wiped every time HF rebuilds or wakes
# from sleep.

FROM python:3.11-slim

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    DEBIAN_FRONTEND=noninteractive \
    PG_MAJOR=15 \
    PGDATA=/tmp/pgdata

WORKDIR /app

# System dependencies. OCCT libs are for ifcopenshell geometry;
# postgresql-15 + postgresql-15-pgvector give us an in-container Postgres
# with pgvector; supervisor multiplexes uvicorn + celery worker + beat.
# postgresql-15 is in Debian Bookworm via the standard pgdg apt repo.
RUN apt-get update && apt-get install -y --no-install-recommends \
        ca-certificates gnupg lsb-release wget \
    && wget -qO- https://www.postgresql.org/media/keys/ACCC4CF8.asc \
        | gpg --dearmor -o /usr/share/keyrings/pgdg.gpg \
    && echo "deb [signed-by=/usr/share/keyrings/pgdg.gpg] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" \
        > /etc/apt/sources.list.d/pgdg.list \
    && apt-get update && apt-get install -y --no-install-recommends \
        build-essential \
        libpq-dev \
        libhdf5-dev \
        libocct-foundation-dev \
        libocct-modeling-algorithms-dev \
        libocct-modeling-data-dev \
        libocct-ocaf-dev \
        libocct-visualization-dev \
        libocct-data-exchange-dev \
        libgl1 \
        libglib2.0-0 \
        libsm6 \
        libxext6 \
        libxrender-dev \
        libgomp1 \
        supervisor \
        curl bzip2 \
        postgresql-${PG_MAJOR} \
        postgresql-${PG_MAJOR}-pgvector \
        postgresql-client-${PG_MAJOR} \
    && rm -rf /var/lib/apt/lists/*

# pythonocc-core has no published wheels on PyPI: install it (and its
# matching Python) into an isolated env via micromamba from conda-forge.
RUN curl -fsSL https://micro.mamba.pm/api/micromamba/linux-64/latest \
        | tar -xj -C /usr/local bin/micromamba \
    && /usr/local/bin/micromamba create -y -p /opt/conda \
        -c conda-forge python=3.11 pythonocc-core \
    && /usr/local/bin/micromamba clean --all --yes

ENV PATH="/opt/conda/bin:/usr/lib/postgresql/${PG_MAJOR}/bin:${PATH}"

# Python dependencies. Copying requirements first keeps Docker's layer
# cache warm across source-only edits.
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

# Application source. The sync script from the monorepo populates this
# directory before docker build.
COPY . .

# Make the entrypoint executable.
RUN chmod +x /app/entrypoint.sh

# HF Spaces run containers as uid 1000. initdb does getpwuid(1000), so
# uid 1000 needs to be a real named user in /etc/passwd, not just a bare
# numeric uid. Without this, Postgres refuses to initialise the data
# directory with "could not look up effective user ID 1000".
RUN groupadd -g 1000 bim \
    && useradd -u 1000 -g 1000 -m -s /bin/bash -d /home/bim bim

# Postgres data dir, app logs, upload staging area and supervisor log dir
# all need to be owned by the bim user.
RUN mkdir -p /app/logs /tmp/uploads /tmp/pgdata /var/log/supervisor \
    && chown -R bim:bim /app /var/log/supervisor /tmp/uploads /tmp/pgdata /home/bim \
    && chmod -R u+rwX /app /var/log/supervisor /tmp/uploads /tmp/pgdata

# Default Postgres connection — Postgres runs in the same container, so
# POSTGRES_HOST=localhost and no TLS. Real secrets (Redis, R2, Groq) come
# from HF Space environment variables, not the image.
ENV HOME=/home/bim \
    POSTGRES_HOST=localhost \
    POSTGRES_PORT=5432 \
    POSTGRES_USER=bim \
    POSTGRES_PASSWORD=bim \
    POSTGRES_DB=bim \
    POSTGRES_SSL= \
    UPLOAD_DIR=/tmp/uploads

USER bim

EXPOSE 7860

# Lightweight HTTP healthcheck against the FastAPI /health endpoint.
HEALTHCHECK --interval=30s --timeout=10s --start-period=120s --retries=3 \
    CMD python -c "import urllib.request, sys; \
sys.exit(0 if urllib.request.urlopen('http://localhost:7860/health', timeout=5).status == 200 else 1)" || exit 1

# Entrypoint boots Postgres in the background, runs migrations, then exec's
# supervisord to manage the application processes.
ENTRYPOINT ["/app/entrypoint.sh"]
CMD ["/usr/bin/supervisord", "-c", "/app/supervisord.conf"]