Perf: Optimize Dockerfile with multi-stage build and shallow clone
Some checks failed
Build and Push Docker Image / build-and-push (push) Failing after 1m58s

This commit is contained in:
2026-01-28 22:52:27 +01:00
parent 8190be4d2f
commit fa0b095640

View File

@@ -1,6 +1,7 @@
FROM node:22-bookworm
# Stage 1: Builder
FROM node:22-bookworm AS builder
# Install system dependencies
# Install build dependencies
RUN apt-get update && apt-get install -y \
curl \
git \
@@ -11,37 +12,54 @@ RUN apt-get update && apt-get install -y \
unzip \
&& rm -rf /var/lib/apt/lists/*
# Install Bun (required for build scripts)
# Install Bun
RUN curl -fsSL https://bun.sh/install | bash
ENV BUN_INSTALL="/root/.bun"
ENV PATH="${BUN_INSTALL}/bin:${PATH}"
# Enable pnpm
RUN corepack enable
WORKDIR /home/node/app
WORKDIR /app
# Clone the official repository
# We clone into the current directory
RUN git clone https://github.com/moltbot/moltbot.git .
# Clone with depth 1 to save space (removes history)
RUN git clone --depth 1 https://github.com/moltbot/moltbot.git .
# Install dependencies
RUN pnpm install --frozen-lockfile
# Build application
# Build Backend
RUN CLAWDBOT_A2UI_SKIP_MISSING=1 pnpm build
# Build UI
# Force pnpm for UI build as per official Dockerfile
ENV CLAWDBOT_PREFER_PNPM=1
RUN pnpm ui:install
RUN pnpm ui:build
# Fix permissions for the node user
RUN chown -R node:node /home/node/app
# Remove devDependencies to reduce size
RUN pnpm prune --prod
# Security: Run as non-root
# Remove .git folder to save space
RUN rm -rf .git
# Stage 2: Runner
FROM node:22-bookworm-slim
WORKDIR /home/node/app
# Install runtime dependencies (lightweight)
# build tools (make, g++) are not needed for runtime
RUN apt-get update && apt-get install -y \
python3 \
socat \
git \
&& rm -rf /var/lib/apt/lists/*
# Set user
USER node
# Start the application
# Copy application from builder
# We copy the entire folder as pruned by 'pnpm prune --prod'
COPY --from=builder --chown=node:node /app /home/node/app
# Start
CMD ["node", "dist/index.js"]