Skip to content

Commit

Permalink
Build Docker container image with wheels
Browse files Browse the repository at this point in the history
Instead of using `uv` virtualenv to build the image, we want to build the wheels
so that the docker app won't need to have dependency on build tools.
  • Loading branch information
michelletran-codecov committed Nov 19, 2024
1 parent 9ad3135 commit 66a4d07
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
32 changes: 25 additions & 7 deletions docker/Dockerfile.requirements
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,30 @@ ENV UV_LINK_MODE=copy \
UV_COMPILE_BYTECODE=1 \
UV_PYTHON_DOWNLOADS=never \
UV_PYTHON=python \
UV_PROJECT_ENVIRONMENT=/worker
UV_PROJECT_ENVIRONMENT=/worker1

# Change the working directory to the `worker` directory
WORKDIR /worker
# Then, add the rest of the project source code and install it
# Installing separately from its dependencies allows optimal layer caching
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --frozen

# Install dependencies
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --frozen --no-install-project
uv export --no-hashes --frozen --format requirements-txt > requirements.txt

RUN grep -v '^-e ' requirements.txt > requirements.remote.txt

# build all remote wheels
RUN pip wheel -w wheels --find-links wheels -r requirements.remote.txt

# build all local packages to wheels
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv build --all-packages --wheel -o wheels


# RUNTIME STAGE - Copy packages from build stage and install runtime dependencies
Expand All @@ -45,8 +59,12 @@ RUN apt-get install -y \
libxslt-dev \
make

WORKDIR /worker
COPY --from=build /worker /worker
COPY --from=build /wheels/ /wheels/

RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv pip install --no-deps --no-index --find-links=wheels wheels/* --system

RUN addgroup --system application \
&& adduser --system codecov --ingroup application --home /home/codecov
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ dependencies = [
"zstandard>=0.23.0",
]

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[tool.setuptools]
py-modules = []

[tool.uv]
dev-dependencies = [
"factory-boy>=3.3.1",
Expand Down

0 comments on commit 66a4d07

Please sign in to comment.