Compare commits

...

1 Commits

Author SHA1 Message Date
jango-blockchained
f8bbe4af6f refactor(docker): optimize Dockerfiles for multi-stage builds and production deployment
- Implement multi-stage builds for main and speech Dockerfiles
- Reduce image size by using slim base images
- Improve dependency installation with frozen lockfile and production flags
- Add resource constraints and healthcheck to speech service Dockerfile
- Enhance build caching and separation of build/runtime dependencies
2025-02-04 19:41:23 +01:00
2 changed files with 60 additions and 30 deletions

View File

@@ -1,14 +1,14 @@
# Use Bun as the base image
FROM oven/bun:1.0.25
FROM oven/bun:1.0.25 as builder
# Set working directory
WORKDIR /app
# Copy package files
COPY package.json ./
# Copy only package files first for better layer caching
COPY package.json bun.lockb ./
# Install dependencies
RUN bun install
# Install dependencies with production flag
RUN bun install --frozen-lockfile --production
# Copy source code
COPY . .
@@ -16,6 +16,19 @@ COPY . .
# Build TypeScript
RUN bun run build
# Create production image
FROM oven/bun:1.0.25-slim
WORKDIR /app
# Copy only the necessary files from builder
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY package.json ./
# Set production environment
ENV NODE_ENV=production
# Expose port
EXPOSE 4000

View File

@@ -1,41 +1,58 @@
FROM python:3.10-slim
# Use Python slim image as builder
FROM python:3.10-slim as builder
# Install system dependencies
# Install build dependencies
RUN apt-get update && apt-get install -y \
git \
build-essential \
portaudio19-dev \
&& rm -rf /var/lib/apt/lists/*
# Create and activate virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Install Python dependencies with specific versions and CPU-only variants
RUN pip install --no-cache-dir torch==2.1.2 torchaudio==2.1.2 --index-url https://download.pytorch.org/whl/cpu
RUN pip install --no-cache-dir faster-whisper==0.10.0 openwakeword==0.4.0 pyaudio==0.2.14 sounddevice==0.4.6
# Create final image
FROM python:3.10-slim
# Copy virtual environment from builder
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Install only runtime dependencies
RUN apt-get update && apt-get install -y \
portaudio19-dev \
python3-pyaudio \
&& rm -rf /var/lib/apt/lists/*
# Install fast-whisper and its dependencies
RUN pip install --no-cache-dir torch torchaudio --index-url https://download.pytorch.org/whl/cpu
RUN pip install --no-cache-dir faster-whisper
# Install wake word detection
RUN pip install --no-cache-dir openwakeword pyaudio sounddevice
# Create directories
RUN mkdir -p /models /audio
# Download the base model by default
# The model will be downloaded automatically when first used
ENV ASR_MODEL=base.en
ENV ASR_MODEL_PATH=/models
# Create wake word model directory
# Models will be downloaded automatically when first used
RUN mkdir -p /models/wake_word
# Create necessary directories
RUN mkdir -p /models/wake_word /audio
# Set working directory
WORKDIR /app
# Copy the wake word detection script
COPY wake_word_detector.py .
# Set environment variables
ENV WHISPER_MODEL_PATH=/models
ENV WAKEWORD_MODEL_PATH=/models/wake_word
ENV PYTHONUNBUFFERED=1
ENV WHISPER_MODEL_PATH=/models \
WAKEWORD_MODEL_PATH=/models/wake_word \
PYTHONUNBUFFERED=1 \
ASR_MODEL=base.en \
ASR_MODEL_PATH=/models
# Run the wake word detection service
CMD ["python", "wake_word_detector.py"]
# Add resource limits to Python
ENV PYTHONMALLOC=malloc \
MALLOC_TRIM_THRESHOLD_=100000 \
PYTHONDEVMODE=1
# Add healthcheck
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD ps aux | grep '[p]ython' || exit 1
# Run the wake word detection service with resource constraints
CMD ["python", "-X", "faulthandler", "wake_word_detector.py"]