Skip to content

Commit

Permalink
🧱 Add support for uv on builder (#359)
Browse files Browse the repository at this point in the history
* 🧱 Add failed status for docker builder when it fails

* 🧱 Add support for uv on builder

* 🚚 Rename Dockerfile.uv to Dockerfile.requirements
  • Loading branch information
estebanx64 authored Sep 25, 2024
1 parent 22b4035 commit 78d9060
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 20 deletions.
18 changes: 0 additions & 18 deletions backend/Dockerfile.build

This file was deleted.

4 changes: 3 additions & 1 deletion backend/Dockerfile.builder
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ COPY ./scripts /app/scripts

COPY ./pyproject.toml ./uv.lock ./alembic.ini /app/

COPY Dockerfile.build /app/Dockerfile
COPY Dockerfile.standard /app/Dockerfile.standard
COPY Dockerfile.requirements /app/Dockerfile.requirements

COPY builder_entrypoint.sh /app/builder_entrypoint.sh
RUN chmod +x /app/builder_entrypoint.sh

Expand Down
31 changes: 31 additions & 0 deletions backend/Dockerfile.requirements
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Dockerfile
FROM python:3.12-slim

WORKDIR /app

# Install uv
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#installing-uv
COPY --from=ghcr.io/astral-sh/uv:0.4.15-alpine /usr/local/bin/uv /bin/uv

# Using uv as pip interface
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#using-the-pip-interface
ENV UV_SYSTEM_PYTHON=1

# Compile bytecode
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#compiling-bytecode
ENV UV_COMPILE_BYTECODE=1

# uv Cache
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#caching
ENV UV_LINK_MODE=copy

COPY requirements.txt .

# Install dependencies
RUN uv pip install -r requirements.txt

ENV PYTHONPATH=/app

COPY . .

CMD ["sh", "-c", "fastapi run --host 0.0.0.0 --port ${PORT:-8000}"]
29 changes: 29 additions & 0 deletions backend/Dockerfile.standard
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Dockerfile
FROM python:3.12-slim

WORKDIR /app

# Install uv
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#installing-uv
COPY --from=ghcr.io/astral-sh/uv:0.4.15-alpine /usr/local/bin/uv /bin/uv

# Using uv as pip interface
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#using-the-pip-interface
ENV UV_SYSTEM_PYTHON=1

# Compile bytecode
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#compiling-bytecode
ENV UV_COMPILE_BYTECODE=1

# uv Cache
# Ref: https://docs.astral.sh/uv/guides/integration/docker/#caching
ENV UV_LINK_MODE=copy

# Install dependencies
RUN uv pip install 'fastapi[standard]'

ENV PYTHONPATH=/app

COPY . .

CMD ["sh", "-c", "fastapi run --host 0.0.0.0 --port ${PORT:-8000}"]
6 changes: 5 additions & 1 deletion backend/app/docker_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,11 @@ def process_message(message: dict[str, Any], session: SessionDep) -> None:
create_ecr_repository(image_tag)

# Copy Dockerfile to build context
shutil.copy("/app/Dockerfile", build_context)
build_files = os.listdir(build_context)
if "requirements.txt" in build_files:
shutil.copy("/app/Dockerfile.requirements", f"{build_context}/Dockerfile")
else:
shutil.copy("/app/Dockerfile.standard", f"{build_context}/Dockerfile")

# Build and push Docker image
sha256 = build_and_push_docker_image(image_tag, build_context, registry_url)
Expand Down

0 comments on commit 78d9060

Please sign in to comment.