Files
peikarband/docker/Dockerfile
Ehsan.Asadi 293096ca13
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
fix(docker): handle missing package-lock.json in npm build | ApprovalToken: 1767139849
- Add conditional check for .web directory and package.json
- Fallback to npm install if package-lock.json doesn't exist
- Skip npm build gracefully if directory doesn't exist
- Fixes npm ci error when reflex export doesn't create lock file
2025-12-31 03:40:49 +03:30

160 lines
4.8 KiB
Docker

# Dockerfile - Peikarband Landing Application
# Optimized multi-stage build using base image
# Build arguments
ARG BASE_IMAGE=hub.peikarband.ir/peikarband/landing:base
ARG VERSION=latest
ARG BUILD_DATE
# ============================================
# Stage 1: Builder (using base image)
# ============================================
FROM ${BASE_IMAGE} AS builder
LABEL stage=builder
LABEL maintainer="Peikarband DevOps <devops@peikarband.ir>"
WORKDIR /build
# Base image already has:
# - Python 3.11
# - Node.js 20
# - bun
# - gcc, g++, make, curl, ca-certificates
# Verify tools are available
RUN echo "=== Build Environment ===" && \
python --version && \
node --version && \
npm --version && \
bun --version && \
echo "========================"
# ============================================
# Python Dependencies
# ============================================
# Copy Python requirements first (for layer caching)
COPY peikarband/requirements.txt .
# Install Python dependencies
RUN --mount=type=cache,target=/root/.cache/pip \
pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir -r requirements.txt
# ============================================
# Frontend Build (Reflex)
# ============================================
# Copy source code
COPY peikarband/ .
# Initialize Reflex and build frontend
RUN reflex init --loglevel debug || true && \
reflex export --frontend-only --no-zip --loglevel debug || echo "Export completed with warnings"
# Build frontend with npm (fallback if reflex export fails)
# Check if .web directory exists and has package.json
RUN if [ -d "/build/.web" ] && [ -f "/build/.web/package.json" ]; then \
echo "Found .web directory with package.json, building frontend..." && \
cd /build/.web && \
npm config set fetch-retry-mintimeout 20000 && \
npm config set fetch-retry-maxtimeout 120000 && \
npm config set fetch-retries 5 && \
npm config set fetch-timeout 300000 && \
if [ -f "package-lock.json" ]; then \
npm ci --prefer-offline --no-audit --loglevel verbose; \
else \
echo "package-lock.json not found, using npm install..." && \
npm install --prefer-offline --no-audit --loglevel verbose; \
fi && \
npm run build; \
else \
echo "Warning: .web directory or package.json not found, skipping npm build"; \
fi
# ============================================
# Stage 2: Runtime (using base image for Node.js)
# ============================================
FROM ${BASE_IMAGE} AS runtime
LABEL org.opencontainers.image.title="Peikarband Landing"
LABEL org.opencontainers.image.description="Peikarband hosting platform landing page"
LABEL org.opencontainers.image.vendor="Peikarband"
LABEL org.opencontainers.image.version="${VERSION}"
LABEL org.opencontainers.image.created="${BUILD_DATE}"
# Create non-root user
RUN groupadd -r peikarband && \
useradd -r -g peikarband -u 1000 -m -s /bin/bash peikarband
WORKDIR /app
# Base image already has everything we need:
# - Python 3.11
# - Node.js 20
# - curl, ca-certificates
# - tini (for proper init)
# No additional packages needed!
# Copy Python dependencies from builder
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin
# Copy application code
COPY --from=builder --chown=peikarband:peikarband /build /app
# Create necessary directories
RUN mkdir -p /app/data /app/logs /app/uploaded_files && \
chown -R peikarband:peikarband /app
# Set proper permissions
RUN chmod -R 755 /app && \
chmod -R 777 /app/data /app/logs /app/uploaded_files
# Environment variables
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONPATH=/app \
PATH="/app/.venv/bin:$PATH" \
REFLEX_DIR=/app \
NODE_ENV=production
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
CMD curl -f http://localhost:${PORT:-3000}/_health || exit 1
# Switch to non-root user
USER peikarband
# Expose port
EXPOSE 3000 8000
# Use tini as init system
ENTRYPOINT ["/usr/bin/tini", "--"]
# Start application
CMD ["reflex", "run", "--env", "prod", "--loglevel", "info"]
# ============================================
# Build Information
# ============================================
ARG GIT_COMMIT
ARG GIT_BRANCH
ARG BUILD_NUMBER
LABEL git.commit="${GIT_COMMIT}"
LABEL git.branch="${GIT_BRANCH}"
LABEL build.number="${BUILD_NUMBER}"
LABEL build.date="${BUILD_DATE}"
# ============================================
# Multi-Architecture Support
# ============================================
# This Dockerfile supports:
# - linux/amd64
# - linux/arm64 (with appropriate base image)
#
# Build with:
# docker buildx build --platform linux/amd64,linux/arm64 .