Files
FacrScraper/Dockerfile
T
Tomas Dvorak aa47f4309f refactor: optimize docker image and implement lightweight fetching
This commit improves the overall efficiency and reliability of the scraper by:

- Optimizing the Dockerfile by reducing layers, using `--no-install-recommends`, and consolidating Playwright installation.
- Adding resource limits (CPU/Memory) to the docker-compose configuration.
- Refactoring `main.go` to remove unused Cloudflare client structures and increasing cache TTL.
- Implementing a `lightweight_fetch` mechanism in `scrapling_fetch.py` using `urllib` to attempt fast requests before falling back to the heavier Scrapling/Playwright engine.
- Adding Cloudflare challenge detection to the lightweight fetcher.
2026-05-11 19:50:59 +02:00

84 lines
2.5 KiB
Docker

# Multi-stage build for Go application with Python/Scrapling support
FROM golang:1.24-alpine AS go-builder
# Install build dependencies
RUN apk add --no-cache git ca-certificates tzdata
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o facr-scraper .
# Python stage for Scrapling
FROM python:3.11-slim AS python-builder
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# Install system dependencies for Playwright
RUN apt-get update && apt-get install -y --no-install-recommends \
wget curl ca-certificates gnupg \
&& rm -rf /var/lib/apt/lists/*
# Create virtual environment and install Scrapling
RUN python -m venv /opt/scrapling
ENV PATH="/opt/scrapling/bin:$PATH"
COPY requirements-scrapling.txt .
RUN pip install --no-cache-dir -r requirements-scrapling.txt
# Install Playwright browsers with deps in one layer
RUN playwright install chromium --with-deps
# Fix Python symlinks
RUN ln -sf /usr/local/bin/python /opt/scrapling/bin/python \
&& ln -sf /usr/local/bin/python /opt/scrapling/bin/python3
# Final stage
FROM python:3.11-slim
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV PATH="/opt/scrapling/bin:$PATH"
# Install runtime dependencies for both Go and Playwright
RUN apt-get update && apt-get install -y --no-install-recommends \
ca-certificates wget curl \
libglib2.0-0 libgobject-2.0-0 libnspr4 libnss3 libdbus-1-3 \
libatk1.0-0 libatk-bridge2.0-0 libcups2 libexpat1 libxcb1 \
libxkbcommon0 libatspi2.0-0 libx11-6 libxcomposite1 libxdamage1 \
libxext6 libxfixes3 libxrandr2 libgbm1 libcairo2 libpango-1.0-0 \
libasound2 \
&& rm -rf /var/lib/apt/lists/*
# Create non-root user
RUN useradd -m -u 1000 scraper
# Copy Go binary
COPY --from=go-builder /app/facr-scraper /usr/local/bin/facr-scraper
# Copy Python environment
COPY --from=python-builder /opt/scrapling /opt/scrapling
# Copy Playwright browser cache
COPY --from=python-builder /root/.cache/ms-playwright /home/scraper/.cache/ms-playwright
# Copy scrapling script
COPY scripts/scrapling_fetch.py /opt/scrapling/scripts/scrapling_fetch.py
# Create cache directory for Playwright
RUN mkdir -p /home/scraper/.cache && chown -R scraper:scraper /home/scraper /opt/scrapling
USER scraper
WORKDIR /home/scraper
# Expose port
EXPOSE 8686
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8686/ || exit 1
CMD ["facr-scraper"]