diff --git a/Dockerfile b/Dockerfile index a944e32fe..0cf51bc41 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,39 +1,34 @@ -#THIS DOCKERFILE RUNS THE WEB API - -# Use the official Python base image +# Use slim uv-based Python image with Python 3.12 FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim -# Enable bytecode compilation -ENV UV_COMPILE_BYTECODE=1 - -# Copy from the cache instead of linking since it's a mounted volume -ENV UV_LINK_MODE=copy +# Environment optimizations for uv and path +ENV UV_COMPILE_BYTECODE=1 \ + UV_LINK_MODE=copy \ + PATH="/app/.venv/bin:$PATH" -# Set the working directory in the container +# Set working directory WORKDIR /app -# Install the project's dependencies using the lockfile and settings +# Install system dependencies in one clean layer +RUN apt-get update && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ + git && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* + +# Copy pyproject + lock file first for layer caching +COPY pyproject.toml uv.lock ./ + +# Install only dependencies (for optimal 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 --no-install-project --no-dev -# Install the Python dependencies -RUN apt-get update -RUN apt-get upgrade -y -RUN apt-get install -y git - -COPY ./chap_core ./chap_core -COPY ./config ./config -COPY ./scripts/seed.py ./scripts/seed.py -COPY ./pyproject.toml . -COPY ./uv.lock . -COPY ./README.md . +# Copy project files +COPY chap_core/ chap_core/ +COPY config/ config/ +COPY scripts/seed.py scripts/seed.py +COPY README.md . -# Then, add the rest of the project source code and install it -# Installing separately from its dependencies allows optimal layer caching +# Install project itself (dev=False, project=True) RUN --mount=type=cache,target=/root/.cache/uv \ uv sync --frozen --no-dev - -# Place executables in the environment at the front of the path -ENV PATH="/app/.venv/bin:$PATH" diff --git a/Dockerfile.inla b/Dockerfile.inla index 40e09d9d1..81bf98d27 100644 --- a/Dockerfile.inla +++ b/Dockerfile.inla @@ -1,41 +1,42 @@ -# If you have issues with platform compatibility and uv, plesae build with -# docker build --platform=linux/amd64 -f Dockerfile.inla . +# ⚠️ NOTE: If you are on Apple Silicon (arm64), build with: +# docker build --platform=linux/amd64 -f Dockerfile.inla . +# INLA base image is only available for amd64 FROM ivargr/r_inla + +# Copy uv and uvx CLI tools from base Python/uv image COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/ -# Enable bytecode compilation and safe volume handling -ENV UV_COMPILE_BYTECODE=1 -ENV UV_LINK_MODE=copy +# Set up env vars for uv and Python +ENV UV_COMPILE_BYTECODE=1 \ + UV_LINK_MODE=copy \ + PATH="/app/.venv/bin:$PATH" # Set working directory WORKDIR /app -# Install all system packages in a single layer +# Install system dependencies in one efficient layer RUN apt-get update && \ - apt-get install -y --no-install-recommends \ + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ git \ libncurses-dev \ libreadline-dev \ libbz2-dev && \ + apt-get clean && \ rm -rf /var/lib/apt/lists/* -# First install only the dependencies using the lockfile (no source code yet) +# Copy only dependency files first for better layer caching +COPY pyproject.toml uv.lock ./ + +# Install Python dependencies only (no dev, no project code yet) 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 --no-dev -# Copy source files -COPY ./chap_core ./chap_core -COPY ./config ./config -COPY ./pyproject.toml . -COPY ./uv.lock . -COPY ./README.md . +# Copy application source files +COPY chap_core/ chap_core/ +COPY config/ config/ +COPY README.md . -# Now install the project (with dependencies already cached) +# Install the project itself now that dependencies are cached RUN --mount=type=cache,target=/root/.cache/uv \ uv sync --frozen --no-dev - -# Final environment path setup -ENV PATH="/app/.venv/bin:$PATH"