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
This commit is contained in:
23
Dockerfile
23
Dockerfile
@@ -1,14 +1,14 @@
|
|||||||
# Use Bun as the base image
|
# Use Bun as the base image
|
||||||
FROM oven/bun:1.0.25
|
FROM oven/bun:1.0.25 as builder
|
||||||
|
|
||||||
# Set working directory
|
# Set working directory
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Copy package files
|
# Copy only package files first for better layer caching
|
||||||
COPY package.json ./
|
COPY package.json bun.lockb ./
|
||||||
|
|
||||||
# Install dependencies
|
# Install dependencies with production flag
|
||||||
RUN bun install
|
RUN bun install --frozen-lockfile --production
|
||||||
|
|
||||||
# Copy source code
|
# Copy source code
|
||||||
COPY . .
|
COPY . .
|
||||||
@@ -16,6 +16,19 @@ COPY . .
|
|||||||
# Build TypeScript
|
# Build TypeScript
|
||||||
RUN bun run build
|
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 port
|
||||||
EXPOSE 4000
|
EXPOSE 4000
|
||||||
|
|
||||||
|
|||||||
@@ -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 \
|
RUN apt-get update && apt-get install -y \
|
||||||
git \
|
git \
|
||||||
build-essential \
|
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 \
|
portaudio19-dev \
|
||||||
python3-pyaudio \
|
python3-pyaudio \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Install fast-whisper and its dependencies
|
# Create necessary directories
|
||||||
RUN pip install --no-cache-dir torch torchaudio --index-url https://download.pytorch.org/whl/cpu
|
RUN mkdir -p /models/wake_word /audio
|
||||||
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
|
|
||||||
|
|
||||||
|
# Set working directory
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Copy the wake word detection script
|
# Copy the wake word detection script
|
||||||
COPY wake_word_detector.py .
|
COPY wake_word_detector.py .
|
||||||
|
|
||||||
# Set environment variables
|
# Set environment variables
|
||||||
ENV WHISPER_MODEL_PATH=/models
|
ENV WHISPER_MODEL_PATH=/models \
|
||||||
ENV WAKEWORD_MODEL_PATH=/models/wake_word
|
WAKEWORD_MODEL_PATH=/models/wake_word \
|
||||||
ENV PYTHONUNBUFFERED=1
|
PYTHONUNBUFFERED=1 \
|
||||||
|
ASR_MODEL=base.en \
|
||||||
|
ASR_MODEL_PATH=/models
|
||||||
|
|
||||||
# Run the wake word detection service
|
# Add resource limits to Python
|
||||||
CMD ["python", "wake_word_detector.py"]
|
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"]
|
||||||
Reference in New Issue
Block a user