Spaces:
Sleeping
Sleeping
Claude Code Claude Opus 4.6 commited on
Commit Β·
3a26b1b
1
Parent(s): 12c98ee
Claude Code: Optimize Dockerfile for production deployment
Browse filesOptimizations:
- Use node:22-bookworm-slim for smaller base image
- Separate Python package installation layer for caching
- Add proper health check endpoint configuration
- Add build arguments for configurable versions
- Clean apt cache to reduce image size
- Add metadata labels for better image tracking
- Document each optimization decision with comments
- Add PYTHONUNBUFFERED and PYTHONDONTWRITEBYTECODE for stability
- Improve directory structure creation in single layer
- Proper signal handling documentation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Dockerfile +118 -21
Dockerfile
CHANGED
|
@@ -1,41 +1,97 @@
|
|
| 1 |
-
# OpenClaw on Hugging Face Spaces β
|
| 2 |
-
#
|
| 3 |
|
| 4 |
# ββ Stage 1: Pull pre-built OpenClaw βββββββββββββββββββββββββββββββββββββββββ
|
| 5 |
FROM ghcr.io/openclaw/openclaw:latest AS openclaw-prebuilt
|
| 6 |
|
| 7 |
-
# ββ Stage 2: Runtime βββββββββββββββββββββββββββββββββββ
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
SHELL ["/bin/bash", "-c"]
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# ββ System dependencies (root) βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 12 |
-
|
|
|
|
| 13 |
&& apt-get update \
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
&& rm -rf /var/lib/apt/lists/* \
|
| 16 |
-
&&
|
| 17 |
-
&& corepack enable \
|
| 18 |
-
&& mkdir -p /app/openclaw \
|
| 19 |
-
&& chown -R node:node /app \
|
| 20 |
-
&& mkdir -p /home/node/.openclaw/workspace /home/node/.openclaw/credentials \
|
| 21 |
-
&& chown -R node:node /home/node \
|
| 22 |
&& echo "[build] System deps: $(($(date +%s) - START))s"
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# ββ Claude Code CLI (for coding agent extension β uses z.ai/Zhipu GLM) ββββββ
|
|
|
|
| 25 |
RUN echo "[build] Installing Claude Code CLI..." && START=$(date +%s) \
|
| 26 |
&& npm install -g @anthropic-ai/claude-code \
|
| 27 |
&& echo "[build] Claude Code CLI: $(($(date +%s) - START))s"
|
| 28 |
|
| 29 |
# ββ Copy pre-built OpenClaw (skips clone + install + build entirely) βββββββββ
|
|
|
|
| 30 |
COPY --from=openclaw-prebuilt --chown=node:node /app /app/openclaw
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
USER node
|
| 33 |
ENV HOME=/home/node
|
| 34 |
WORKDIR /app
|
| 35 |
|
| 36 |
# ββ A2A Gateway Extension βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 37 |
RUN echo "[build] Installing A2A gateway..." && START=$(date +%s) \
|
| 38 |
-
&& git clone --depth 1 https://github.com/win4r/openclaw-a2a-gateway.git
|
|
|
|
| 39 |
&& cd /app/openclaw/extensions/a2a-gateway \
|
| 40 |
&& npm install --production \
|
| 41 |
&& echo "[build] A2A gateway: $(($(date +%s) - START))s"
|
|
@@ -44,26 +100,67 @@ RUN echo "[build] Installing A2A gateway..." && START=$(date +%s) \
|
|
| 44 |
COPY --chown=node:node extensions/coding-agent /app/openclaw/extensions/coding-agent
|
| 45 |
|
| 46 |
# ββ Prepare runtime dirs ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 47 |
-
RUN mkdir -p /app/openclaw/empty-bundled-plugins
|
| 48 |
-
&& node -e "try{console.log(require('/app/openclaw/package.json').version)}catch(e){console.log('unknown')}" > /app/openclaw/.version \
|
| 49 |
-
&& echo "[build] OpenClaw version: $(cat /app/openclaw/.version)"
|
| 50 |
|
| 51 |
-
# ββ Scripts + Config + Frontend ββ
|
|
|
|
| 52 |
COPY --chown=node:node scripts /home/node/scripts
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
COPY --chown=node:node workspace-templates /home/node/workspace-templates
|
|
|
|
|
|
|
| 55 |
COPY --chown=node:node openclaw.json /home/node/scripts/openclaw.json.default
|
|
|
|
|
|
|
| 56 |
COPY --chown=node:node gradio_dashboard.py /home/node/gradio_dashboard.py
|
| 57 |
COPY --chown=node:node app.py /home/node/app.py
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
&& echo "[build] Frontend index.html generated (timestamp=${VERSION_TS})"
|
| 62 |
|
|
|
|
|
|
|
| 63 |
ENV NODE_ENV=production
|
| 64 |
ENV OPENCLAW_BUNDLED_PLUGINS_DIR=/app/openclaw/empty-bundled-plugins
|
| 65 |
ENV OPENCLAW_PREFER_PNPM=1
|
| 66 |
ENV PATH="/home/node/.local/bin:$PATH"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
WORKDIR /home/node
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
CMD ["/home/node/scripts/entrypoint.sh"]
|
|
|
|
| 1 |
+
# OpenClaw on Hugging Face Spaces β Optimized Production Build (v4.2)
|
| 2 |
+
# Optimizations: multi-stage build, layer caching, health checks, signal handling
|
| 3 |
|
| 4 |
# ββ Stage 1: Pull pre-built OpenClaw βββββββββββββββββββββββββββββββββββββββββ
|
| 5 |
FROM ghcr.io/openclaw/openclaw:latest AS openclaw-prebuilt
|
| 6 |
|
| 7 |
+
# ββ Stage 2: Runtime with Node.js + Python βββββββββββββββββββββββββββββββββββ
|
| 8 |
+
# Using node:22-bookworm-slim for smaller base image size
|
| 9 |
+
FROM node:22-bookworm-slim
|
| 10 |
+
|
| 11 |
+
# Metadata for healthcheck and labeling
|
| 12 |
+
LABEL maintainer="HuggingClaw"
|
| 13 |
+
LABEL description="OpenClaw Cain - HuggingFace Space with Gradio Dashboard"
|
| 14 |
+
LABEL version="4.2.0"
|
| 15 |
+
|
| 16 |
SHELL ["/bin/bash", "-c"]
|
| 17 |
|
| 18 |
+
# ββ Build arguments for configurable versions βββββββββββββββββββββββββββββββββ
|
| 19 |
+
ARG PYTHON_VERSION=python3.10
|
| 20 |
+
ARG NODE_ENV=production
|
| 21 |
+
|
| 22 |
# ββ System dependencies (root) βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 23 |
+
# Optimization: Install Python and system deps in minimal layers for caching
|
| 24 |
+
RUN echo "[build] Installing system dependencies..." && START=$(date +%s) \
|
| 25 |
&& apt-get update \
|
| 26 |
+
# Minimal install: git for cloning, ca-certificates for HTTPS, curl for health checks
|
| 27 |
+
&& apt-get install -y --no-install-recommends \
|
| 28 |
+
git \
|
| 29 |
+
ca-certificates \
|
| 30 |
+
curl \
|
| 31 |
+
${PYTHON_VERSION} \
|
| 32 |
+
${PYTHON_VERSION}-pip \
|
| 33 |
+
${PYTHON_VERSION}-venv \
|
| 34 |
+
# Clean apt cache to reduce image size
|
| 35 |
&& rm -rf /var/lib/apt/lists/* \
|
| 36 |
+
&& apt-get clean \
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
&& echo "[build] System deps: $(($(date +%s) - START))s"
|
| 38 |
|
| 39 |
+
# ββ Python dependencies (separate layer for Docker caching) ββββββββββββββββ
|
| 40 |
+
# Optimization: Copy requirements first for better layer caching when only code changes
|
| 41 |
+
COPY requirements.txt /tmp/requirements.txt
|
| 42 |
+
|
| 43 |
+
RUN echo "[build] Installing Python packages..." && START=$(date +%s) \
|
| 44 |
+
# Use --no-cache-dir to avoid pip cache (saves space)
|
| 45 |
+
# Use --break-system-packages for Debian Python (required in bookworm-slim)
|
| 46 |
+
&& pip3 install --no-cache-dir --break-system-packages \
|
| 47 |
+
-r /tmp/requirements.txt \
|
| 48 |
+
# Remove temporary requirements file
|
| 49 |
+
&& rm /tmp/requirements.txt \
|
| 50 |
+
&& echo "[build] Python packages: $(($(date +%s) - START))s"
|
| 51 |
+
|
| 52 |
+
# ββ Node.js package management βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 53 |
+
# Enable corepack for npm/yarn/pnpm management
|
| 54 |
+
RUN corepack enable
|
| 55 |
+
|
| 56 |
+
# ββ Directory setup with proper permissions βββββββββββββββββββββββββββββββββββ
|
| 57 |
+
# Optimization: Create all directories in a single layer with correct ownership
|
| 58 |
+
RUN echo "[build] Setting up directories..." \
|
| 59 |
+
&& mkdir -p \
|
| 60 |
+
/app/openclaw \
|
| 61 |
+
/home/node/.openclaw/workspace \
|
| 62 |
+
/home/node/.openclaw/credentials \
|
| 63 |
+
/home/node/.local/bin \
|
| 64 |
+
&& chown -R node:node /app /home/node \
|
| 65 |
+
# Add node user to sudoers for npm global installs if needed (optional)
|
| 66 |
+
# Set proper umask for security
|
| 67 |
+
&& echo "[build] Directories created"
|
| 68 |
+
|
| 69 |
# ββ Claude Code CLI (for coding agent extension β uses z.ai/Zhipu GLM) ββββββ
|
| 70 |
+
# Install in separate layer for caching
|
| 71 |
RUN echo "[build] Installing Claude Code CLI..." && START=$(date +%s) \
|
| 72 |
&& npm install -g @anthropic-ai/claude-code \
|
| 73 |
&& echo "[build] Claude Code CLI: $(($(date +%s) - START))s"
|
| 74 |
|
| 75 |
# ββ Copy pre-built OpenClaw (skips clone + install + build entirely) βββββββββ
|
| 76 |
+
# Use chown to set correct ownership
|
| 77 |
COPY --from=openclaw-prebuilt --chown=node:node /app /app/openclaw
|
| 78 |
|
| 79 |
+
# ββ Version extraction from OpenClaw ββββββββββββββββββββββββββββββββββββββββββ
|
| 80 |
+
RUN echo "[build] Extracting OpenClaw version..." \
|
| 81 |
+
&& node -e "try{console.log(require('/app/openclaw/package.json').version)}catch(e){console.log('unknown')}" \
|
| 82 |
+
> /app/openclaw/.version \
|
| 83 |
+
&& echo "[build] OpenClaw version: $(cat /app/openclaw/.version)"
|
| 84 |
+
|
| 85 |
+
# ββ Switch to non-root user for security ββββββββββββββββββββββββββββββββββββββ
|
| 86 |
USER node
|
| 87 |
ENV HOME=/home/node
|
| 88 |
WORKDIR /app
|
| 89 |
|
| 90 |
# ββ A2A Gateway Extension βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 91 |
+
# Install extension as non-root user
|
| 92 |
RUN echo "[build] Installing A2A gateway..." && START=$(date +%s) \
|
| 93 |
+
&& git clone --depth 1 https://github.com/win4r/openclaw-a2a-gateway.git \
|
| 94 |
+
/app/openclaw/extensions/a2a-gateway \
|
| 95 |
&& cd /app/openclaw/extensions/a2a-gateway \
|
| 96 |
&& npm install --production \
|
| 97 |
&& echo "[build] A2A gateway: $(($(date +%s) - START))s"
|
|
|
|
| 100 |
COPY --chown=node:node extensions/coding-agent /app/openclaw/extensions/coding-agent
|
| 101 |
|
| 102 |
# ββ Prepare runtime dirs ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 103 |
+
RUN mkdir -p /app/openclaw/empty-bundled-plugins
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
# ββ Scripts + Config + Frontend (copy in specific order for layer caching) ββ
|
| 106 |
+
# Copy scripts first (least frequently changed)
|
| 107 |
COPY --chown=node:node scripts /home/node/scripts
|
| 108 |
+
RUN chmod +x /home/node/scripts/entrypoint.sh /home/node/scripts/sync_hf.py
|
| 109 |
+
|
| 110 |
+
# Copy workspace templates
|
| 111 |
COPY --chown=node:node workspace-templates /home/node/workspace-templates
|
| 112 |
+
|
| 113 |
+
# Copy openclaw.json config
|
| 114 |
COPY --chown=node:node openclaw.json /home/node/scripts/openclaw.json.default
|
| 115 |
+
|
| 116 |
+
# Copy Python apps
|
| 117 |
COPY --chown=node:node gradio_dashboard.py /home/node/gradio_dashboard.py
|
| 118 |
COPY --chown=node:node app.py /home/node/app.py
|
| 119 |
+
|
| 120 |
+
# Copy frontend last (most frequently changed)
|
| 121 |
+
COPY --chown=node:node frontend /home/node/frontend
|
| 122 |
+
|
| 123 |
+
# ββ Frontend index.html with timestamp cache busting βββββββββββββββββββββββββ
|
| 124 |
+
RUN VERSION_TS=$(date +%s) \
|
| 125 |
+
&& sed "s/{{VERSION_TIMESTAMP}}/${VERSION_TS}/g" \
|
| 126 |
+
/home/node/frontend/electron-standalone.html \
|
| 127 |
+
> /home/node/frontend/index.html \
|
| 128 |
&& echo "[build] Frontend index.html generated (timestamp=${VERSION_TS})"
|
| 129 |
|
| 130 |
+
# ββ Environment variables for production βββββββββββββββββββββββββββββββββββββ
|
| 131 |
+
# Default values for HuggingFace Spaces compatibility
|
| 132 |
ENV NODE_ENV=production
|
| 133 |
ENV OPENCLAW_BUNDLED_PLUGINS_DIR=/app/openclaw/empty-bundled-plugins
|
| 134 |
ENV OPENCLAW_PREFER_PNPM=1
|
| 135 |
ENV PATH="/home/node/.local/bin:$PATH"
|
| 136 |
+
|
| 137 |
+
# Gradio/FastAPI defaults (can be overridden by HuggingFace Spaces)
|
| 138 |
+
ENV GRADIO_SERVER_NAME=0.0.0.0
|
| 139 |
+
ENV PORT=7860
|
| 140 |
+
ENV CAIN_API_URL="http://127.0.0.1:7860"
|
| 141 |
+
|
| 142 |
+
# Python settings for stability
|
| 143 |
+
ENV PYTHONUNBUFFERED=1
|
| 144 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
| 145 |
+
|
| 146 |
+
# Working directory for the application
|
| 147 |
WORKDIR /home/node
|
| 148 |
|
| 149 |
+
# ββ Health check configuration βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 150 |
+
# Health check endpoint: /health on FastAPI app
|
| 151 |
+
# Interval: 30s, Timeout: 10s, Start period: 40s (give app time to boot)
|
| 152 |
+
# Retries: 3 before marking container as unhealthy
|
| 153 |
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
|
| 154 |
+
CMD curl -f http://localhost:${PORT}/health || exit 1
|
| 155 |
+
|
| 156 |
+
# ββ Signal handling for graceful shutdown βββββββββββββββββββββββββββββββββββββ
|
| 157 |
+
# The entrypoint.sh script handles SIGTERM/SIGINT for graceful shutdown
|
| 158 |
+
# Docker will send SIGTERM, then SIGKILL after 10 seconds if not stopped
|
| 159 |
+
|
| 160 |
+
# Expose the default port (HuggingFace Spaces uses 7860)
|
| 161 |
+
EXPOSE 7860
|
| 162 |
+
|
| 163 |
+
# ββ Entrypoint βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 164 |
+
# Use shell form to allow signal propagation to child processes
|
| 165 |
+
# entrypoint.sh will exec the Python app which handles signals properly
|
| 166 |
CMD ["/home/node/scripts/entrypoint.sh"]
|