qbhf2 commited on
Commit
e3b6888
·
verified ·
1 Parent(s): f71e2b8

Create Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +114 -0
Dockerfile ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # =========================
2
+ # CUDA + Python 3.10 base
3
+ # =========================
4
+ FROM nvidia/cuda:12.1.1-devel-ubuntu22.04
5
+
6
+ ENV DEBIAN_FRONTEND=noninteractive \
7
+ PIP_DISABLE_PIP_VERSION_CHECK=1 \
8
+ PIP_NO_CACHE_DIR=1 \
9
+ HOME=/home/user
10
+
11
+ # System packages
12
+ RUN apt-get update && apt-get install -y --no-install-recommends \
13
+ python3.10 python3.10-dev python3-pip python-is-python3 \
14
+ curl git ffmpeg libsm6 libxext6 bash \
15
+ && rm -rf /var/lib/apt/lists/*
16
+
17
+ # Ensure latest pip for py3.10 (sometimes older in apt)
18
+ RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python3.10
19
+
20
+ # Create non-root user
21
+ RUN useradd -m -u 1000 user && \
22
+ mkdir -p /home/user && chown -R user:user /home/user
23
+
24
+ # -----------------------------
25
+ # Runtime bootstrap (start.sh)
26
+ # -----------------------------
27
+ # Expects Space settings / env at RUNTIME (not build time):
28
+ # Secret: GIT_TOKEN (GitHub PAT with repo read access)
29
+ # Variable: GIT_REPO (e.g. chaous/render OR https://github.com/chaous/render.git)
30
+ # Variable: GIT_REF (optional, default "main")
31
+ # Variable: GIT_SUBDIR (optional, if app.py is not at repo root)
32
+ # Variable: TARGET_ROOT (optional, default /home/user/app)
33
+ # Variable: VENV_DIR (optional, default /home/user/venv)
34
+ RUN printf '%s\n' \
35
+ '#!/usr/bin/env bash' \
36
+ 'set -euo pipefail' \
37
+ 'export GIT_TERMINAL_PROMPT=0' \
38
+ '' \
39
+ 'PY=python3.10' \
40
+ 'TARGET_ROOT="${TARGET_ROOT:-/home/user/app}"' \
41
+ 'VENV_DIR="${VENV_DIR:-/home/user/venv}"' \
42
+ 'REPO="${GIT_REPO:-}"' \
43
+ 'REF="${GIT_REF:-main}"' \
44
+ 'SUBDIR="${GIT_SUBDIR:-}"' \
45
+ '' \
46
+ 'if [[ -z "${REPO}" || -z "${GIT_TOKEN:-}" ]]; then' \
47
+ ' echo "[error] GIT_REPO and GIT_TOKEN must be set as Space variables/secrets." >&2; exit 1' \
48
+ 'fi' \
49
+ '' \
50
+ '# Normalize to full HTTPS URL' \
51
+ 'if [[ "${REPO}" == *"://"* ]]; then' \
52
+ ' BASE_URL="${REPO}"' \
53
+ 'else' \
54
+ ' BASE_URL="https://github.com/${REPO}.git"' \
55
+ 'fi' \
56
+ '' \
57
+ '# Build auth URL: for GitHub use token-in-URL (avoids prompts). For other hosts use header.' \
58
+ 'USE_HEADER=0' \
59
+ 'if [[ "${BASE_URL}" == https://github.com/* ]]; then' \
60
+ ' AUTH_URL="${BASE_URL/https:\/\//https:\/\/x-access-token:${GIT_TOKEN}@}"' \
61
+ 'else' \
62
+ ' AUTH_URL="${BASE_URL}"' \
63
+ ' USE_HEADER=1' \
64
+ 'fi' \
65
+ '' \
66
+ 'echo "[info] Cloning ${BASE_URL}@${REF} into ${TARGET_ROOT}..."' \
67
+ 'rm -rf "${TARGET_ROOT}" "${VENV_DIR}"' \
68
+ 'mkdir -p "${TARGET_ROOT}"' \
69
+ 'if [[ "${USE_HEADER}" -eq 1 ]]; then' \
70
+ ' git -c http.extraHeader="Authorization: Bearer ${GIT_TOKEN}" clone --depth 1 --branch "${REF}" "${AUTH_URL}" "${TARGET_ROOT}"' \
71
+ 'else' \
72
+ ' git clone --depth 1 --branch "${REF}" "${AUTH_URL}" "${TARGET_ROOT}"' \
73
+ 'fi' \
74
+ '' \
75
+ 'if [[ -n "${SUBDIR}" && -d "${TARGET_ROOT}/${SUBDIR}" ]]; then' \
76
+ ' cd "${TARGET_ROOT}/${SUBDIR}"' \
77
+ 'else' \
78
+ ' cd "${TARGET_ROOT}"' \
79
+ 'fi' \
80
+ '' \
81
+ 'echo "[info] Creating venv at ${VENV_DIR} ..." ' \
82
+ '"${PY}" -m venv "${VENV_DIR}"' \
83
+ 'source "${VENV_DIR}/bin/activate"' \
84
+ 'python -m pip install --upgrade pip wheel' \
85
+ '' \
86
+ '# Install requirements if present. If torch* is listed, add CUDA 12.1 index.' \
87
+ 'if [[ -f requirements.txt ]]; then' \
88
+ ' if grep -qiE "^(torch|torchvision|torchaudio)" requirements.txt; then' \
89
+ ' echo "[info] Installing requirements with PyTorch CUDA index (cu121)..."' \
90
+ ' pip install --extra-index-url https://download.pytorch.org/whl/cu121 -r requirements.txt' \
91
+ ' else' \
92
+ ' pip install -r requirements.txt' \
93
+ ' fi' \
94
+ 'fi' \
95
+ '' \
96
+ 'APP_ENTRY="app.py"' \
97
+ 'if [[ -f "gradio_app.py" ]]; then APP_ENTRY="gradio_app.py"; fi' \
98
+ 'if [[ ! -f "${APP_ENTRY}" ]]; then echo "[error] No app.py or gradio_app.py found." >&2; ls -la; exit 1; fi' \
99
+ '' \
100
+ 'echo "[info] Starting ${APP_ENTRY} ..." ' \
101
+ 'exec "${VENV_DIR}/bin/python" "${APP_ENTRY}"' \
102
+ > /usr/local/bin/start.sh \
103
+ && chmod 755 /usr/local/bin/start.sh \
104
+ && chown root:root /usr/local/bin/start.sh
105
+
106
+ # Switch to non-root for runtime
107
+ USER user
108
+ ENV PATH="/home/user/.local/bin:${PATH}" \
109
+ GRADIO_SERVER_NAME=0.0.0.0 \
110
+ PORT=7860
111
+ WORKDIR /home/user
112
+
113
+ EXPOSE 7860
114
+ CMD ["/usr/local/bin/start.sh"]