Spaces:
Paused
Paused
Create Dockerfile
Browse files- Dockerfile +120 -0
Dockerfile
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -------- Base Python with libs Blender needs (CPU headless) --------
|
| 2 |
+
FROM python:3.11-slim-bookworm
|
| 3 |
+
|
| 4 |
+
ENV DEBIAN_FRONTEND=noninteractive
|
| 5 |
+
|
| 6 |
+
# System deps for Blender GUI-less rendering
|
| 7 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 8 |
+
ca-certificates wget bzip2 xz-utils \
|
| 9 |
+
libglib2.0-0 libx11-6 libxi6 libxxf86vm1 libxrender1 libxfixes3 \
|
| 10 |
+
libxkbcommon0 libxrandr2 libasound2 libxinerama1 libsm6 libice6 \
|
| 11 |
+
libgl1 libegl1 libglu1-mesa libdbus-1-3 libxcb1 \
|
| 12 |
+
git curl \
|
| 13 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 14 |
+
|
| 15 |
+
# Grab a local copy of model-viewer so we can inline it (no external script loads)
|
| 16 |
+
RUN mkdir -p /app/static && \
|
| 17 |
+
wget -q -O /app/static/model-viewer.min.js \
|
| 18 |
+
https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js
|
| 19 |
+
|
| 20 |
+
# -------- Install official Blender (includes OpenImageDenoise) --------
|
| 21 |
+
ARG BLENDER_VERSION=4.1.1
|
| 22 |
+
ARG BLENDER_MAJOR=4.1
|
| 23 |
+
RUN wget -q https://download.blender.org/release/Blender${BLENDER_MAJOR}/blender-${BLENDER_VERSION}-linux-x64.tar.xz \
|
| 24 |
+
&& tar -xJf blender-${BLENDER_VERSION}-linux-x64.tar.xz -C /opt \
|
| 25 |
+
&& rm blender-${BLENDER_VERSION}-linux-x64.tar.xz \
|
| 26 |
+
&& ln -s /opt/blender-${BLENDER_VERSION}-linux-x64/blender /usr/local/bin/blender
|
| 27 |
+
|
| 28 |
+
# Pillow inside Blender's embedded Python (render.py imports PIL inside Blender)
|
| 29 |
+
RUN /opt/blender-${BLENDER_VERSION}-linux-x64/${BLENDER_MAJOR}/python/bin/python3.11 -m ensurepip && \
|
| 30 |
+
/opt/blender-${BLENDER_VERSION}-linux-x64/${BLENDER_MAJOR}/python/bin/python3.11 -m pip install --no-cache-dir Pillow
|
| 31 |
+
|
| 32 |
+
# -------- Python deps (base) --------
|
| 33 |
+
WORKDIR /app
|
| 34 |
+
COPY requirements.txt /app/requirements.txt
|
| 35 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 36 |
+
|
| 37 |
+
# -------- Fallback App code (used only if GIT_TOKEN/GIT_REPO not set) --------
|
| 38 |
+
COPY app.py render.py /app/
|
| 39 |
+
|
| 40 |
+
# -------- Runtime bootstrap to pull private repo safely --------
|
| 41 |
+
# Expects:
|
| 42 |
+
# - Secret: GIT_TOKEN
|
| 43 |
+
# - Variable: GIT_REPO (e.g. owner/private-repo)
|
| 44 |
+
# - Variable: GIT_REF (optional, default "main")
|
| 45 |
+
# - Variable: GIT_SUBDIR (optional, e.g. "apps/serviceA")
|
| 46 |
+
RUN set -eux; \
|
| 47 |
+
cat > /usr/local/bin/start.sh << 'EOF'; \
|
| 48 |
+
#!/usr/bin/env bash
|
| 49 |
+
set -euo pipefail
|
| 50 |
+
|
| 51 |
+
export BLENDER_BIN="${BLENDER_BIN:-blender}"
|
| 52 |
+
|
| 53 |
+
TARGET_ROOT="/srv/app"
|
| 54 |
+
mkdir -p "$TARGET_ROOT"
|
| 55 |
+
|
| 56 |
+
REPO="${GIT_REPO:-}"
|
| 57 |
+
REF="${GIT_REF:-main}"
|
| 58 |
+
SUBDIR="${GIT_SUBDIR:-}"
|
| 59 |
+
|
| 60 |
+
use_bundled_fallback() {
|
| 61 |
+
echo "[info] Using bundled /app fallback (no private repo configured)."
|
| 62 |
+
rsync -a /app/ "${TARGET_ROOT}/"
|
| 63 |
+
cd "${TARGET_ROOT}"
|
| 64 |
+
exec python app.py
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
if [[ -z "${REPO}" ]] || [[ -z "${GIT_TOKEN:-}" ]]; then
|
| 68 |
+
use_bundled_fallback
|
| 69 |
+
fi
|
| 70 |
+
|
| 71 |
+
echo "[info] Downloading ${REPO}@${REF} tarball from GitHub..."
|
| 72 |
+
ARCHIVE_URL="https://api.github.com/repos/${REPO}/tarball/${REF}"
|
| 73 |
+
|
| 74 |
+
# Download without leaking token into image layers (runtime only)
|
| 75 |
+
# Token is passed in header; -q to avoid verbose logs
|
| 76 |
+
if ! wget -q --header="Authorization: Bearer ${GIT_TOKEN}" -O /tmp/repo.tar.gz "${ARCHIVE_URL}"; then
|
| 77 |
+
echo "[warn] Download failed; falling back to bundled app."
|
| 78 |
+
use_bundled_fallback
|
| 79 |
+
fi
|
| 80 |
+
|
| 81 |
+
# Extract and normalize to TARGET_ROOT (strip top dir)
|
| 82 |
+
rm -rf "${TARGET_ROOT:?}/"*
|
| 83 |
+
tar -xzf /tmp/repo.tar.gz -C "${TARGET_ROOT}" --strip-components=1
|
| 84 |
+
rm -f /tmp/repo.tar.gz
|
| 85 |
+
|
| 86 |
+
if [[ -n "${SUBDIR}" ]]; then
|
| 87 |
+
if [[ -d "${TARGET_ROOT}/${SUBDIR}" ]]; then
|
| 88 |
+
TARGET_ROOT="${TARGET_ROOT}/${SUBDIR}"
|
| 89 |
+
else
|
| 90 |
+
echo "[warn] GIT_SUBDIR=${SUBDIR} not found in repo; continuing from repo root."
|
| 91 |
+
fi
|
| 92 |
+
fi
|
| 93 |
+
|
| 94 |
+
# If the repo ships its own requirements, install them now
|
| 95 |
+
if [[ -f "${TARGET_ROOT}/requirements.txt" ]]; then
|
| 96 |
+
echo "[info] Installing repo requirements..."
|
| 97 |
+
pip install --no-cache-dir -r "${TARGET_ROOT}/requirements.txt"
|
| 98 |
+
fi
|
| 99 |
+
|
| 100 |
+
cd "${TARGET_ROOT}"
|
| 101 |
+
|
| 102 |
+
# Basic sanity check
|
| 103 |
+
if [[ ! -f "app.py" ]]; then
|
| 104 |
+
echo "[warn] app.py not found in repo path; falling back to bundled app."
|
| 105 |
+
use_bundled_fallback
|
| 106 |
+
fi
|
| 107 |
+
|
| 108 |
+
echo "[info] Starting app from private repo..."
|
| 109 |
+
exec python app.py
|
| 110 |
+
EOF
|
| 111 |
+
RUN chmod +x /usr/local/bin/start.sh
|
| 112 |
+
|
| 113 |
+
# Hugging Face Spaces env
|
| 114 |
+
ENV PORT=7860 \
|
| 115 |
+
GRADIO_SERVER_NAME=0.0.0.0 \
|
| 116 |
+
GRADIO_SERVER_PORT=7860 \
|
| 117 |
+
BLENDER_BIN=blender
|
| 118 |
+
|
| 119 |
+
EXPOSE 7860
|
| 120 |
+
CMD ["/usr/local/bin/start.sh"]
|