File size: 2,889 Bytes
b83660d
 
15ae77a
 
 
 
 
 
 
df2bc50
 
 
 
 
 
 
15ae77a
 
 
 
 
df2bc50
15ae77a
 
 
 
 
51800ac
 
15ae77a
2cb4410
6375010
 
15ae77a
2cb4410
 
 
 
15ae77a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b1d6a3
8d15910
51800ac
15ae77a
 
 
 
 
 
 
3d75728
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
# Use the official Python 3.12 slim image as the base
FROM python:3.12-slim

# --- Install System Dependencies, uv, and Node.js/npm/npx ---
# Install curl (for uv script), nodejs & npm (for npx), git (optional, but sometimes needed by deps)
# Install uv using its official script
# Install npx globally using npm (often included with recent npm, but explicit install is safer)
# Clean up apt cache to reduce image size
RUN apt-get update && \
    apt-get install -y --no-install-recommends curl git ca-certificates gnupg && \
    mkdir -p /etc/apt/keyrings && \
    curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg && \
    NODE_MAJOR=20 && \
    echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list && \
    apt-get update && \
    apt-get install nodejs -y && \
    curl -LsSf https://astral.sh/uv/install.sh | sh && \
    mv /root/.local/bin/uv /usr/local/bin/uv && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*


# --- Python Dependency Installation (as root for system-wide access) ---
# Create cache directory for uv
RUN mkdir -p /.cache/uv

# Copy only the pyproject.toml file first to leverage Docker layer caching
COPY pyproject.toml ./
COPY README.md ./

# Install Python packages as root using uv sync
RUN uv sync && \
    rm -rf /.uv

# Create and switch to non-root user
RUN useradd -m -u 1000 user && \
    chown -R user:user /.cache/uv

USER user

# --- Environment Setup ---
# Set the home directory for the user
ENV HOME=/home/user
# Add user's local bin to PATH (though we installed system-wide with uv sync --system)
# Ensure Python output is sent straight to terminal without being buffered
# Set the default port expected by Hugging Face Spaces for web apps
ENV PATH="/home/user/.local/bin:$PATH" \
    PYTHONUNBUFFERED=1 \
    PORT=7860

# Set the working directory for the application inside the user's home
WORKDIR $HOME/app

# --- Application Code ---
# Copy the rest of the application files needed at runtime
# Ensure the non-root user owns these files
# Specific copies are better for caching than 'COPY . .' after dependency install
COPY --chown=user:user ./chainlit_ui.py ./chainlit_ui.py
COPY --chown=user:user ./mcp.json ./mcp.json
COPY --chown=user:user ./src ./src
COPY --chown=user:user ./public ./public
COPY --chown=user:user ./chainlit.yaml ./chainlit.yaml
COPY --chown=user:user ./chainlit.md ./chainlit.md

# --- Runtime ---
# Expose the port Chainlit will run on (standard HF Spaces port)
EXPOSE 7860

# Define the command to run the Chainlit application
# Listen on all interfaces (0.0.0.0) on the designated port
# Do NOT use the --watch (-w) flag in production
CMD ["uv","run", "chainlit", "run", "chainlit_ui.py", "--host", "0.0.0.0", "--port", "7860"]