ARG UBUNTU_IMAGE=public.ecr.aws/docker/library/ubuntu@sha256:19cad59657037cffa0ac9a48107019b768900ff1071140965827513ac866bd5e
FROM ${UBUNTU_IMAGE} AS base
ARG DEBIAN_FRONTEND=noninteractive
RUN sed -i 's|http://ports.ubuntu.com/ubuntu-ports|http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports|g; s|http://archive.ubuntu.com/ubuntu|http://mirrors.tuna.tsinghua.edu.cn/ubuntu|g; s|http://security.ubuntu.com/ubuntu|http://mirrors.tuna.tsinghua.edu.cn/ubuntu|g' /etc/apt/sources.list.d/ubuntu.sources \
    && apt-get -o Acquire::Retries=2 -o Acquire::http::Timeout=30 update \
    && apt-get install -y --no-install-recommends python3 python3-venv ca-certificates libgl1 libglib2.0-0t64 libgomp1 \
    && rm -rf /var/lib/apt/lists/* \
    && python3 -m venv /opt/venv
ENV PATH=/opt/venv/bin:$PATH \
    PYTHONUNBUFFERED=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PADDLE_PDX_MODEL_SOURCE=BOS \
    PADDLE_PDX_DISABLE_MODEL_SOURCE_CHECK=True \
    OMP_NUM_THREADS=1 \
    OPENBLAS_NUM_THREADS=1
WORKDIR /app
ARG PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple
COPY requirements.lock .
RUN python -m pip install --no-cache-dir -r requirements.lock && python -m pip check
RUN python -c "import paddle; paddle.utils.run_check()"
RUN python -c "import onnxruntime as ort; assert 'CPUExecutionProvider' in ort.get_available_providers(); print('ONNX Runtime', ort.__version__, 'CPU provider ready')"

FROM base AS service
COPY OCR.yaml .
EXPOSE 8080
CMD ["paddlex", "--serve", "--pipeline", "/app/OCR.yaml", "--engine", "onnxruntime", "--device", "cpu", "--host", "0.0.0.0", "--port", "8080"]
