mirror of
https://github.com/wahyd4/you-music.git
synced 2026-08-08 20:59:47 +10:00
105 lines
2.7 KiB
Docker
105 lines
2.7 KiB
Docker
# Optimized Multi-stage build - Smaller image size
|
|
# Target: < 500MB (down from 1.12GB)
|
|
|
|
# Stage 1: Frontend Builder (Alpine-based)
|
|
FROM node:20-alpine AS frontend-builder
|
|
|
|
WORKDIR /app/frontend
|
|
|
|
# Copy package files and install ONLY production dependencies
|
|
COPY frontend/package.json frontend/package-lock.json ./
|
|
COPY frontend/.npmrc ./
|
|
|
|
# Install dependencies without dev packages
|
|
RUN npm ci --omit=dev
|
|
|
|
# Copy source and build
|
|
COPY frontend/ ./
|
|
RUN npm run build && \
|
|
# Remove source maps to save space
|
|
find dist -name "*.map" -type f -delete
|
|
|
|
|
|
# Stage 2: Python Dependencies (Slim-based)
|
|
FROM python:3.13-slim AS python-builder
|
|
|
|
WORKDIR /app
|
|
|
|
# Install build dependencies temporarily
|
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
gcc \
|
|
g++ \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
# Copy requirements and install to a specific location
|
|
COPY backend/requirements.txt .
|
|
|
|
# Install Python packages to /opt/venv to copy to final stage
|
|
RUN python -m venv /opt/venv && \
|
|
/opt/venv/bin/pip install --no-cache-dir --upgrade pip && \
|
|
/opt/venv/bin/pip install --no-cache-dir -r requirements.txt
|
|
|
|
|
|
# Stage 3: Final Runtime Image (Minimal)
|
|
FROM python:3.13-slim
|
|
|
|
# Install ONLY runtime dependencies (no build tools)
|
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
# FFmpeg for audio processing
|
|
ffmpeg \
|
|
# Curl for health checks
|
|
curl \
|
|
# Clean up
|
|
&& rm -rf /var/lib/apt/lists/* \
|
|
&& apt-get clean \
|
|
&& rm -rf /tmp/* /var/tmp/*
|
|
|
|
WORKDIR /app
|
|
|
|
# Copy Python virtual environment from builder
|
|
COPY --from=python-builder /opt/venv /opt/venv
|
|
|
|
# Copy backend code (only what's needed)
|
|
COPY backend/main.py ./backend/
|
|
COPY backend/alembic.ini ./backend/
|
|
COPY backend/alembic/ ./backend/alembic/
|
|
COPY backend/app/ ./backend/app/
|
|
|
|
# Copy startup scripts
|
|
COPY backend/run-migrations.sh backend/start.sh ./backend/
|
|
RUN chmod +x ./backend/*.sh
|
|
|
|
# Copy frontend build from builder stage
|
|
COPY --from=frontend-builder /app/frontend/dist ./backend/static
|
|
|
|
# Create data directories
|
|
RUN mkdir -p /app/data/music \
|
|
/app/data/uploads \
|
|
/app/data/temp \
|
|
/app/data/cache/artists \
|
|
/app/data/cache/artist_images
|
|
|
|
# Environment variables
|
|
ENV PATH="/opt/venv/bin:$PATH" \
|
|
PYTHONUNBUFFERED=1 \
|
|
PYTHONPATH=/app/backend \
|
|
PYTHONDONTWRITEBYTECODE=1 \
|
|
DATABASE_URL=sqlite+aiosqlite:///./data/youmusic.db \
|
|
MUSIC_DIR=/app/data/music \
|
|
UPLOAD_DIR=/app/data/uploads \
|
|
TEMP_DIR=/app/data/temp \
|
|
BASE_DIR=/app
|
|
|
|
# Expose port
|
|
EXPOSE 8000
|
|
|
|
# Health check
|
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
|
CMD curl -f http://localhost:8000/ || exit 1
|
|
|
|
# Set working directory to backend
|
|
WORKDIR /app/backend
|
|
|
|
# Run migrations and start the application
|
|
CMD ["/app/backend/start.sh"]
|