# syntax=docker/dockerfile:1.7

FROM ubuntu:24.04 AS build

ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        cmake \
        g++ \
        gcc \
        libc6-dev \
        make \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /src
COPY vendor/lw.PPOCR.C/ ./

# Runtime-only avoids Python/model-conversion dependencies. The vendored Tiny
# ONNX models are loaded directly by lw.PPOCR.C.
RUN cmake -S . -B build \
        -DCMAKE_BUILD_TYPE=Release \
        -DLW_RUNTIME_ONLY=ON \
        -DLW_BUILD_HTTP_DEMO=OFF \
        -DBUILD_TESTING=OFF \
    && cmake --build build --target lw_ppocr_c --parallel "$(nproc)" \
    && mkdir -p /out \
    && g++ -std=c++11 -O2 \
        -Iinclude \
        -Iexamples/http-server/third_party \
        examples/http-server/main.cpp \
        build/liblw_ppocr_c_static.a \
        -pthread -lm \
        -o /out/lw-http-ocr

FROM ubuntu:24.04

ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        ca-certificates \
        curl \
        libc6 \
        libgcc-s1 \
        libstdc++6 \
        tzdata \
    && rm -rf /var/lib/apt/lists/* \
    && groupadd --gid 10001 ocr \
    && useradd --uid 10001 --gid ocr --no-create-home --shell /usr/sbin/nologin ocr

WORKDIR /app
COPY --from=build /out/lw-http-ocr /usr/local/bin/lw-http-ocr
COPY vendor/lw.PPOCR.C/models/ppocrv6-tiny/ /app/models/
COPY vendor/lw.PPOCR.C/examples/http-server/www/ /app/www/

ENV TZ=Asia/Shanghai
USER 10001:10001
EXPOSE 8787

HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
    CMD curl -fsS http://127.0.0.1:8787/health >/dev/null || exit 1

ENTRYPOINT ["/usr/local/bin/lw-http-ocr"]
CMD ["--host", "0.0.0.0", "--port", "8787", "--models", "/app/models", "--www", "/app/www", "--ocr-workers", "2", "--rec-max-width", "960"]
