Skip to content

Commit 54648ea

Browse files
build (Dockerfile.web): improve caching
- move source code beneath venv - add deps stage for caching - copy venv before source code in final stage
1 parent 38f8201 commit 54648ea

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

Dockerfile.web

+20-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ RUN uv python install "$PYTHON_VERSION"
1818

1919
WORKDIR /app
2020

21-
COPY ./app .
2221
COPY pyproject.toml .
2322

2423
# venv
@@ -29,7 +28,22 @@ ENV PATH="$VENV/bin:$PATH"
2928
RUN uv venv $UV_PROJECT_ENVIRONMENT \
3029
&& uv pip install -r pyproject.toml
3130

32-
FROM python:${PYTHON_VERSION}-slim-bookworm as runner
31+
COPY ./app .
32+
33+
FROM python:${PYTHON_VERSION}-slim-bookworm as deps
34+
35+
# avoid stuck build due to user prompt
36+
ARG DEBIAN_FRONTEND=noninteractive
37+
38+
# install dependencies
39+
RUN apt-get -qq update \
40+
&& apt-get -qq install \
41+
--no-install-recommends -y \
42+
curl \
43+
lsof \
44+
&& rm -rf /var/lib/apt/lists/*
45+
46+
FROM deps as runner
3347

3448
# set timezone
3549
ENV TZ=${TZ:-"America/Chicago"}
@@ -51,17 +65,6 @@ ENV PYTHONPATH=/app
5165
# https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker/blob/master/README.md#web_concurrency
5266
ENV WEB_CONCURRENCY=2
5367

54-
# avoid stuck build due to user prompt
55-
ARG DEBIAN_FRONTEND=noninteractive
56-
57-
# install dependencies
58-
RUN apt-get -qq update \
59-
&& apt-get -qq install \
60-
--no-install-recommends -y \
61-
curl \
62-
lsof \
63-
&& rm -rf /var/lib/apt/lists/*
64-
6568
# add non-root user
6669
ARG UID=10001
6770
RUN adduser \
@@ -80,9 +83,12 @@ COPY --from=builder --chown=${USER_NAME}:${USER_NAME} /python /python
8083

8184
WORKDIR /app
8285

83-
COPY --chown=${USER_NAME} ./app .
86+
# Copy venv first (has better caching properties)
8487
COPY --from=builder --chown=${USER_NAME} "$VENV" "$VENV"
8588

89+
# Copy application code last
90+
COPY --chown=${USER_NAME} ./app .
91+
8692
ARG PORT=${PORT:-3100}
8793
EXPOSE $PORT
8894

0 commit comments

Comments
 (0)