Skip to content

Commit 7e934aa

Browse files
committed
Dockerfiles + compose: perf(docker): faster image builds and container startup
1 parent 0e361a2 commit 7e934aa

3 files changed

Lines changed: 37 additions & 12 deletions

File tree

docker/dev/Dockerfile

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,30 @@ FROM python:3.11-slim
33

44
WORKDIR /app
55

6-
# Use apt cache mount to speed up system package installation across builds
6+
# apt cache mounts keep downloaded .debs across builds
77
RUN rm -f /etc/apt/apt.conf.d/docker-clean; echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
88
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
99
--mount=type=cache,target=/var/lib/apt \
1010
apt-get update && apt-get install -y \
1111
curl \
1212
libgl1 \
1313
libglib2.0-0 \
14-
libxcb1
14+
libxcb1 \
15+
libpq-dev \
16+
build-essential \
17+
g++
18+
19+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
1520

1621
COPY requirements.txt .
1722

18-
# Use pip cache mount so it remembers downloaded wheels
19-
RUN --mount=type=cache,target=/root/.cache/pip \
20-
pip install -r requirements.txt
23+
RUN --mount=type=cache,target=/root/.cache/uv \
24+
UV_TORCH_BACKEND=cpu \
25+
uv pip install --system -r requirements.txt
26+
27+
# Bake a hash of the deps the image was built with. The entrypoint compares this
28+
# against the live (bind-mounted) requirements.txt to decide whether to reinstall.
29+
RUN sha256sum requirements.txt | cut -d' ' -f1 > /opt/req_hash
2130

2231
ENV PYTHONPATH=/app
2332

docker/dev/compose.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ services:
1717
interval: 10s
1818
timeout: 5s
1919
retries: 5
20+
start_period: 30s
21+
start_interval: 1s
2022

2123
ollama:
2224
image: ollama/ollama:latest
@@ -33,6 +35,7 @@ services:
3335
timeout: 5s
3436
retries: 5
3537
start_period: 30s
38+
start_interval: 2s
3639

3740
whisper:
3841
image: onerahmet/openai-whisper-asr-webservice:latest
@@ -53,6 +56,7 @@ services:
5356
timeout: 5s
5457
retries: 5
5558
start_period: 60s
59+
start_interval: 3s
5660

5761
app:
5862
build:

docker/prod/Dockerfile

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
1+
# ---- builder ----
12
FROM python:3.11-slim AS builder
23

3-
WORKDIR /build
4+
RUN apt-get update && apt-get install -y --no-install-recommends \
5+
build-essential g++ libpq-dev && \
6+
rm -rf /var/lib/apt/lists/*
7+
8+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
49

10+
WORKDIR /build
511
COPY requirements.txt .
6-
RUN pip install --no-cache-dir --prefix=/install -r requirements.txt
712

13+
RUN --mount=type=cache,target=/root/.cache/uv \
14+
UV_TORCH_BACKEND=cpu \
15+
uv pip install --system -r requirements.txt
816

17+
# ---- runtime ----
918
FROM python:3.11-slim
1019

1120
WORKDIR /app
1221

13-
RUN apt-get update && apt-get install -y \
22+
RUN apt-get update && apt-get install -y --no-install-recommends \
1423
curl \
1524
libgl1 \
1625
libglib2.0-0 \
1726
libxcb1 \
18-
&& rm -rf /var/lib/apt/lists/*
27+
libpq5 && \
28+
rm -rf /var/lib/apt/lists/*
1929

20-
COPY --from=builder /install /usr/local
30+
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
31+
COPY --from=builder /usr/local/bin /usr/local/bin
2132

22-
# Copy only app code, not data/ temp/ tests/ docs/ etc.
2333
COPY app/ ./app/
2434
COPY requirements.txt .
2535
COPY docker/entrypoint.sh /entrypoint.sh
2636

37+
# Bake deps hash so the entrypoint can skip the redundant install when unchanged.
38+
RUN sha256sum requirements.txt | cut -d' ' -f1 > /opt/req_hash
39+
2740
ENV PYTHONPATH=/app
2841

29-
# Data dirs created here; actual storage comes from mounted volumes at runtime.
3042
RUN mkdir -p /data/db /data/uploads && chmod +x /entrypoint.sh
3143

3244
EXPOSE 8000

0 commit comments

Comments
 (0)