Skip to content

Commit f361d1e

Browse files
authored
Merge pull request #133 from huridocs/docker-size
Docker files improved & optimized Image size reduced Support for old and new GPUs added
2 parents fec7467 + 090da2a commit f361d1e

12 files changed

Lines changed: 338 additions & 90 deletions

.dockerignore

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
11
/venv/
22
/.venv/
33
.git
4+
.cursor/
5+
.pytest_cache/
6+
.ruff_cache/
7+
.mypy_cache/
8+
.idea/
9+
.ipynb_checkpoints/
10+
__pycache__/
11+
**/.pytest_cache/
12+
**/.ruff_cache/
13+
**/.mypy_cache/
14+
**/.ipynb_checkpoints/
15+
**/__pycache__/
16+
*.py[cod]
17+
**/*.py[cod]
18+
*.egg-info/
19+
**/*.egg-info/
20+
**/tests/
21+
**/test_*.py
22+
*.ipynb
23+
**/*.ipynb
24+
.coverage
25+
**/.coverage
26+
.env
27+
.env.*
28+
.github/
29+
Dockerfile*
30+
docker-compose*.yml
31+
dev-requirements.txt
32+
README.md
433
/detectron2/
534
/images/
6-
/test_pdfs/
35+
/models/
36+
/pdf_outputs/
37+
/test_pdfs/

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,5 @@ cython_debug/
165165
/pdf_outputs/
166166
/detectron2/
167167
/ocr/
168+
169+
.docker.gpu.env

Dockerfile

Lines changed: 106 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,113 @@
1-
FROM pytorch/pytorch:2.4.0-cuda11.8-cudnn9-runtime
2-
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
3-
4-
RUN apt-get update
5-
RUN apt-get install --fix-missing -y -q --no-install-recommends libgomp1 ffmpeg libsm6 pdftohtml libxext6 git ninja-build g++ qpdf pandoc curl
6-
7-
8-
RUN apt-get install -y ocrmypdf
9-
RUN apt-get install -y tesseract-ocr-fra
10-
RUN apt-get install -y tesseract-ocr-spa
11-
RUN apt-get install -y tesseract-ocr-deu
12-
RUN apt-get install -y tesseract-ocr-ara
13-
RUN apt-get install -y tesseract-ocr-mya
14-
RUN apt-get install -y tesseract-ocr-hin
15-
RUN apt-get install -y tesseract-ocr-tam
16-
RUN apt-get install -y tesseract-ocr-tha
17-
RUN apt-get install -y tesseract-ocr-chi-sim
18-
RUN apt-get install -y tesseract-ocr-tur
19-
RUN apt-get install -y tesseract-ocr-ukr
20-
RUN apt-get install -y tesseract-ocr-ell
21-
RUN apt-get install -y tesseract-ocr-rus
22-
RUN apt-get install -y tesseract-ocr-kor
23-
RUN apt-get install -y tesseract-ocr-kor-vert
24-
25-
26-
RUN mkdir -p /app/src
27-
RUN mkdir -p /app/models
28-
29-
RUN addgroup --system python && adduser --system --group python
30-
RUN chown -R python:python /app
31-
USER python
1+
ARG BUILDER_IMAGE=nvidia/cuda:12.6.0-cudnn-devel-ubuntu24.04
2+
ARG TORCH_INDEX_URL=https://download.pytorch.org/whl/cu126
3+
ARG FORCE_CUDA=1
4+
ARG TORCH_CUDA_ARCH_LIST="6.1;7.0;7.5;8.0;8.6;8.9;9.0"
5+
6+
FROM ${BUILDER_IMAGE} AS builder
7+
8+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /usr/local/bin/
329

33-
ENV VIRTUAL_ENV=/app/.venv
34-
RUN python -m venv $VIRTUAL_ENV
35-
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
10+
ARG TORCH_INDEX_URL
11+
ARG FORCE_CUDA
12+
ARG TORCH_CUDA_ARCH_LIST
3613

37-
COPY requirements.txt requirements.txt
38-
RUN uv pip install --upgrade pip
39-
RUN uv pip install \
40-
-r requirements.txt \
41-
--extra-index-url https://download.pytorch.org/whl/cu128 \
42-
--index-strategy unsafe-best-match
14+
ENV VIRTUAL_ENV=/app/.venv \
15+
PATH="/app/.venv/bin:$PATH" \
16+
DEBIAN_FRONTEND=noninteractive \
17+
PIP_DISABLE_PIP_VERSION_CHECK=1 \
18+
FORCE_CUDA=${FORCE_CUDA} \
19+
TORCH_CUDA_ARCH_LIST=${TORCH_CUDA_ARCH_LIST}
20+
21+
RUN apt-get update && \
22+
apt-get install -y --no-install-recommends \
23+
ca-certificates \
24+
python3.12 \
25+
python3.12-venv \
26+
python3.12-dev \
27+
git \
28+
ninja-build \
29+
g++ \
30+
&& rm -rf /var/lib/apt/lists/*
4331

4432
WORKDIR /app
4533

46-
RUN cd src; git clone https://github.com/facebookresearch/detectron2;
47-
RUN cd src/detectron2; git checkout b599f139756bd3646a26a909caf86a1a159e53a7; python setup.py build develop
48-
RUN uv pip install pycocotools==2.0.11
34+
RUN python3.12 -m venv "$VIRTUAL_ENV"
4935

50-
COPY ./start.sh ./start.sh
51-
COPY ./src/. ./src
52-
COPY ./models/. ./models/
53-
RUN python src/download_models.py
36+
COPY requirements.txt .
37+
38+
RUN uv pip install --no-cache --python "$VIRTUAL_ENV/bin/python" \
39+
--extra-index-url "${TORCH_INDEX_URL}" \
40+
--index-strategy unsafe-best-match \
41+
-r requirements.txt
42+
43+
RUN git init /tmp/detectron2 && \
44+
cd /tmp/detectron2 && \
45+
git remote add origin https://github.com/facebookresearch/detectron2 && \
46+
git fetch --depth 1 origin b599f139756bd3646a26a909caf86a1a159e53a7 && \
47+
git checkout FETCH_HEAD && \
48+
uv pip install --no-cache --no-build-isolation \
49+
--python "$VIRTUAL_ENV/bin/python" . && \
50+
rm -rf /tmp/detectron2
51+
52+
RUN uv pip install --no-cache --python "$VIRTUAL_ENV/bin/python" pycocotools==2.0.11
53+
54+
55+
FROM ubuntu:24.04 AS runtime
5456

55-
ENV PYTHONPATH "${PYTHONPATH}:/app/src"
56-
ENV TRANSFORMERS_VERBOSITY=error
57-
ENV TRANSFORMERS_NO_ADVISORY_WARNINGS=1
57+
ENV VIRTUAL_ENV=/app/.venv \
58+
PATH="/app/.venv/bin:$PATH" \
59+
HF_HOME=/app/models/.cache/huggingface \
60+
PYTHONPATH=/app/src \
61+
PYTHONUNBUFFERED=1 \
62+
TRANSFORMERS_VERBOSITY=error \
63+
TRANSFORMERS_NO_ADVISORY_WARNINGS=1 \
64+
DEBIAN_FRONTEND=noninteractive \
65+
NVIDIA_VISIBLE_DEVICES=all \
66+
NVIDIA_DRIVER_CAPABILITIES=compute,utility
67+
68+
RUN apt-get update && \
69+
apt-get install -y --no-install-recommends \
70+
adduser \
71+
ca-certificates \
72+
python3.12 \
73+
ffmpeg \
74+
libgomp1 \
75+
libsm6 \
76+
libxext6 \
77+
ocrmypdf \
78+
pandoc \
79+
pdftohtml \
80+
qpdf \
81+
tesseract-ocr-ara \
82+
tesseract-ocr-chi-sim \
83+
tesseract-ocr-deu \
84+
tesseract-ocr-ell \
85+
tesseract-ocr-fra \
86+
tesseract-ocr-hin \
87+
tesseract-ocr-kor \
88+
tesseract-ocr-kor-vert \
89+
tesseract-ocr-mya \
90+
tesseract-ocr-rus \
91+
tesseract-ocr-spa \
92+
tesseract-ocr-tam \
93+
tesseract-ocr-tha \
94+
tesseract-ocr-tur \
95+
tesseract-ocr-ukr \
96+
&& rm -rf /var/lib/apt/lists/*
97+
98+
RUN mkdir -p /app/src /app/models && \
99+
addgroup --system python && \
100+
adduser --system --group --home /app python && \
101+
chown -R python:python /app
102+
103+
WORKDIR /app
104+
105+
COPY --from=builder --chown=python:python /app/.venv /app/.venv
106+
COPY --chown=python:python --chmod=755 ./start.sh ./start.sh
107+
COPY --chown=python:python ./src/download_models.py ./src/download_models.py
108+
COPY --chown=python:python ./src/configuration.py ./src/configuration.py
109+
110+
USER python
111+
112+
RUN python src/download_models.py
113+
COPY --chown=python:python ./src/. ./src

Dockerfile.gradio

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,29 @@
1-
FROM python:3.11-slim
1+
FROM python:3.12-slim
22

3-
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
3+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /usr/local/bin/
44

5-
# Install minimal system dependencies
65
RUN apt-get update && \
76
apt-get install -y --no-install-recommends curl && \
8-
apt-get clean && \
97
rm -rf /var/lib/apt/lists/*
108

11-
RUN mkdir -p /app/src/drivers
9+
RUN mkdir -p /app/src/drivers && \
10+
addgroup --system python && \
11+
adduser --system --group --home /app python && \
12+
chown -R python:python /app
1213

13-
RUN addgroup --system python && adduser --system --group python
14-
RUN chown -R python:python /app
14+
WORKDIR /app
1515

16-
# Copy requirements before switching user
17-
COPY requirements-gradio.txt requirements-gradio.txt
16+
COPY --chown=python:python requirements-gradio.txt requirements-gradio.txt
1817

1918
USER python
2019

21-
ENV VIRTUAL_ENV=/app/.venv
22-
RUN python -m venv $VIRTUAL_ENV
23-
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
24-
25-
# Set UV cache directory to a location the user can write to
26-
ENV UV_CACHE_DIR=/app/.cache/uv
27-
RUN uv pip install --upgrade pip
28-
RUN uv pip install -r requirements-gradio.txt
29-
30-
WORKDIR /app
31-
32-
COPY ./src/drivers/gradio_app.py ./src/drivers/gradio_app.py
20+
ENV VIRTUAL_ENV=/app/.venv \
21+
PATH="/app/.venv/bin:$PATH" \
22+
PYTHONUNBUFFERED=1 \
23+
PYTHONPATH=/app/src
3324

34-
ENV PYTHONUNBUFFERED=1
35-
ENV PYTHONPATH "${PYTHONPATH}:/app/src"
25+
RUN python -m venv "$VIRTUAL_ENV" && \
26+
uv pip install --no-cache --python "$VIRTUAL_ENV/bin/python" \
27+
-r requirements-gradio.txt
3628

29+
COPY --chown=python:python ./src/drivers/gradio_app.py ./src/drivers/gradio_app.py

Dockerfile.ollama

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
FROM ollama/ollama:latest
22

3-
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
4-
53
ENV OLLAMA_HOST=0.0.0.0:11434
64

75
EXPOSE 11434
86

97
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
10-
CMD curl -f http://localhost:11434/api/tags || exit 1
8+
CMD OLLAMA_HOST=http://127.0.0.1:11434 ollama list || exit 1

Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ else
88
endif
99
ifeq ($(HAS_GPU), 1)
1010
@echo "NVIDIA GPU detected, starting with translation support (GPU-enabled Ollama)"
11+
bash ./select_gpu_stack.sh .docker.gpu.env
1112
@echo "Starting Ollama GPU container first..."
12-
docker compose -f docker-compose-gpu.yml up -d ollama-gpu
13+
docker compose --env-file .docker.gpu.env -f docker-compose-gpu.yml up -d ollama-gpu
1314
@echo "Waiting for Ollama to be healthy..."
1415
@timeout=60; while [ $$timeout -gt 0 ]; do \
1516
if docker inspect --format='{{.State.Health.Status}}' ollama-service-gpu 2>/dev/null | grep -q "healthy"; then \
@@ -24,7 +25,7 @@ ifeq ($(HAS_GPU), 1)
2425
echo "Warning: Ollama GPU container may not be fully healthy yet, but continuing..."; \
2526
fi
2627
@echo "Starting all services with translation support..."
27-
docker compose -f docker-compose-gpu.yml up --build pdf-document-layout-analysis-gpu pdf-document-layout-analysis-gui-gpu
28+
docker compose --env-file .docker.gpu.env -f docker-compose-gpu.yml up --build pdf-document-layout-analysis-gpu pdf-document-layout-analysis-gui-gpu
2829
else
2930
@echo "No NVIDIA GPU detected, starting with translation support (CPU Ollama)"
3031
@echo "Starting Ollama container first..."
@@ -82,6 +83,7 @@ start_detached:
8283
start_detached_gpu:
8384
mkdir -p ./models
8485
@echo "Starting in detached mode with GPU"
85-
RESTART_IF_NO_GPU=true docker compose -f docker-compose-gpu.yml up --build -d pdf-document-layout-analysis-gpu
86+
bash ./select_gpu_stack.sh .docker.gpu.env
87+
RESTART_IF_NO_GPU=true docker compose --env-file .docker.gpu.env -f docker-compose-gpu.yml up --build -d pdf-document-layout-analysis-gpu
8688
@echo "Main application started in background. Check status with: docker compose ps"
8789
@echo "View logs with: docker compose logs -f pdf-document-layout-analysis-gpu"

docker-compose-gpu.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ services:
2222
build:
2323
context: .
2424
dockerfile: Dockerfile
25+
args:
26+
BUILDER_IMAGE: ${BUILDER_IMAGE:-nvidia/cuda:12.6.0-cudnn-devel-ubuntu24.04}
27+
TORCH_INDEX_URL: ${TORCH_INDEX_URL:-https://download.pytorch.org/whl/cu126}
28+
FORCE_CUDA: "1"
29+
TORCH_CUDA_ARCH_LIST: ${TORCH_CUDA_ARCH_LIST:-6.1;7.0;7.5;8.0;8.6;8.9;9.0}
2530
ports:
2631
- "5060:5060"
2732
depends_on:
@@ -62,4 +67,4 @@ services:
6267

6368
networks:
6469
pdf-analysis-network:
65-
driver: bridge
70+
driver: bridge

docker-compose.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ services:
88
ports:
99
- "11434:11434"
1010
healthcheck:
11-
test: ["CMD", "curl", "-f", "http://localhost:11434/api/tags"]
11+
test: ["CMD-SHELL", "OLLAMA_HOST=http://127.0.0.1:11434 ollama list"]
1212
interval: 30s
1313
timeout: 10s
1414
retries: 3
@@ -24,6 +24,10 @@ services:
2424
build:
2525
context: .
2626
dockerfile: Dockerfile
27+
args:
28+
BUILDER_IMAGE: ubuntu:24.04
29+
TORCH_INDEX_URL: https://download.pytorch.org/whl/cpu
30+
FORCE_CUDA: "0"
2731
ports:
2832
- "5060:5060"
2933
depends_on:

justfile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ help:
2929
@echo " just free_up_space - Free up system space"
3030
@echo ""
3131
@echo "💡 Tip: 'just start' launches API (port 5060), UI (port 7860) and translation support"
32+
@echo "💡 GPU stack override: GPU_STACK_PROFILE=legacy|nextgen just start"
3233

3334
install:
3435
. .venv/bin/activate; pip install -Ur requirements.txt
@@ -58,8 +59,9 @@ start:
5859
mkdir -p ./models
5960
if [ {{HAS_GPU}} -eq 1 ]; then
6061
echo "NVIDIA GPU detected, starting with translation support (GPU-enabled Ollama)"
62+
bash select_gpu_stack.sh .docker.gpu.env
6163
echo "Starting Ollama GPU container first..."
62-
docker compose -f docker-compose-gpu.yml up -d ollama-gpu
64+
docker compose --env-file .docker.gpu.env -f docker-compose-gpu.yml up -d ollama-gpu
6365
echo "Waiting for Ollama to be healthy..."
6466
timeout=60
6567
while [ $timeout -gt 0 ]; do
@@ -75,7 +77,7 @@ start:
7577
echo "Warning: Ollama GPU container may not be fully healthy yet, but continuing..."
7678
fi
7779
echo "Starting all services with translation support..."
78-
docker compose -f docker-compose-gpu.yml up --build pdf-document-layout-analysis-gpu pdf-document-layout-analysis-gui-gpu
80+
docker compose --env-file .docker.gpu.env -f docker-compose-gpu.yml up --build pdf-document-layout-analysis-gpu pdf-document-layout-analysis-gui-gpu
7981
else
8082
echo "No NVIDIA GPU detected, starting with translation support (CPU Ollama)"
8183
echo "Starting Ollama container first..."
@@ -125,7 +127,8 @@ start_no_translation:
125127
mkdir -p ./models
126128
if [ {{HAS_GPU}} -eq 1 ]; then \
127129
echo "NVIDIA GPU detected, using docker-compose-gpu.yml"; \
128-
docker compose -f docker-compose-gpu.yml up --build pdf-document-layout-analysis-gpu pdf-document-layout-analysis-gui-gpu; \
130+
bash select_gpu_stack.sh .docker.gpu.env; \
131+
docker compose --env-file .docker.gpu.env -f docker-compose-gpu.yml up --build pdf-document-layout-analysis-gpu pdf-document-layout-analysis-gui-gpu; \
129132
else \
130133
echo "No NVIDIA GPU detected, using docker-compose.yml"; \
131134
docker compose -f docker-compose.yml up --build pdf-document-layout-analysis pdf-document-layout-analysis-gui; \
@@ -170,7 +173,8 @@ start_detached:
170173
start_detached_gpu:
171174
mkdir -p ./models
172175
@echo "Starting in detached mode with GPU"
173-
RESTART_IF_NO_GPU=true docker compose -f docker-compose-gpu.yml up --build -d pdf-document-layout-analysis-gpu
176+
bash select_gpu_stack.sh .docker.gpu.env
177+
RESTART_IF_NO_GPU=true docker compose --env-file .docker.gpu.env -f docker-compose-gpu.yml up --build -d pdf-document-layout-analysis-gpu
174178
@echo "Main application started in background. Check status with: docker compose ps"
175179
@echo "View logs with: docker compose logs -f pdf-document-layout-analysis-gpu"
176180

0 commit comments

Comments
 (0)