|
| 1 | +# Docker image management |
| 2 | + |
| 3 | +## Layer structure |
| 4 | + |
| 5 | +The Dockerfile is ordered to maximize cache reuse: |
| 6 | + |
| 7 | +``` |
| 8 | +Layer 1: system packages (gcc, libpq-dev, etc.) — changes rarely |
| 9 | +Layer 2: PyTorch CPU-only wheel — changes rarely |
| 10 | +Layer 3: remaining Python dependencies — changes when pyproject.toml changes |
| 11 | +Layer 4: source code (src/, app/, scripts/) — changes frequently |
| 12 | +``` |
| 13 | + |
| 14 | +When you change only application code, Docker reuses layers 1–3 and only rebuilds layer 4. |
| 15 | +This makes iterative rebuilds take seconds rather than minutes. |
| 16 | + |
| 17 | +## PyTorch CPU-only wheel |
| 18 | + |
| 19 | +The default PyTorch pip wheel includes CUDA binaries for GPU support (~800 MB). |
| 20 | +This server has no GPU, so we install the CPU-only variant (~200 MB): |
| 21 | + |
| 22 | +```dockerfile |
| 23 | +RUN pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cpu |
| 24 | +``` |
| 25 | + |
| 26 | +This alone cuts ~600 MB from the image size and significantly speeds up the first build. |
| 27 | + |
| 28 | +## Dependency caching trick |
| 29 | + |
| 30 | +pip needs the package source to resolve dependencies. To avoid copying `src/` before |
| 31 | +the pip install step (which would bust the cache on every source change), we stub out |
| 32 | +the package with an empty `__init__.py`: |
| 33 | + |
| 34 | +```dockerfile |
| 35 | +COPY pyproject.toml . |
| 36 | +RUN mkdir -p src/mm_forum && touch src/mm_forum/__init__.py && \ |
| 37 | + pip install --no-cache-dir ".[app]" |
| 38 | + |
| 39 | +# Real source copied after — only invalidates layers below, not pip install |
| 40 | +COPY src/ src/ |
| 41 | +``` |
| 42 | + |
| 43 | +## Build times |
| 44 | + |
| 45 | +| Scenario | Expected time | |
| 46 | +|---|---| |
| 47 | +| First ever build (cold cache) | 4–8 min (downloading PyTorch + all deps) | |
| 48 | +| After `pyproject.toml` change | 3–5 min (reinstalls deps) | |
| 49 | +| After source-only change | ~30 s (only COPY layers rebuild) | |
| 50 | + |
| 51 | +## Rebuilding on the server |
| 52 | + |
| 53 | +```bash |
| 54 | +cd ~/projects/mm-forums-vector-db |
| 55 | +git pull |
| 56 | +docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d --build |
| 57 | +``` |
| 58 | + |
| 59 | +After a source-only change the `--build` step completes in ~30 s. |
| 60 | + |
| 61 | +## Image size vs embedding model |
| 62 | + |
| 63 | +If build time or image size is still a concern, switch to the OpenAI embedder — |
| 64 | +it removes sentence-transformers and PyTorch from the image entirely: |
| 65 | + |
| 66 | +```bash |
| 67 | +# In .env on the server |
| 68 | +EMBEDDING_MODEL=openai |
| 69 | +OPENAI_API_KEY=<your-key> |
| 70 | +``` |
| 71 | + |
| 72 | +Trade-off: embeddings cost money per token and require an internet call per batch, |
| 73 | +vs free local inference with a ~1 GB image. |
0 commit comments