From e11223fa7576040052ca56375988efeb78df0fac Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Tue, 23 Sep 2025 09:43:08 +0300 Subject: [PATCH] feat(docker): optimize build times --- Dockerfile | 51 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index 83f081e..cd9be6c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,34 +1,45 @@ -FROM rust:1.90-slim-trixie AS builder +FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef WORKDIR /app -COPY Cargo.toml Cargo.lock ./ + +FROM chef AS planner COPY . . +RUN cargo chef prepare --recipe-path recipe.json + + +FROM chef AS builder-rs +COPY --from=planner /app/recipe.json recipe.json +# Build dependencies - this is the caching Docker layer! +RUN cargo chef cook --release --recipe-path recipe.json +# Build application +COPY . . +RUN cargo build --release + + +FROM ghcr.io/astral-sh/uv:trixie-slim AS builder-py +ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy + +# Configure the Python directory so it is consistent +ENV UV_PYTHON_INSTALL_DIR=/python + +# Only use the managed Python version +ENV UV_PYTHON_PREFERENCE=only-managed + +# Install Python before the project for caching +RUN uv python install 3.13 RUN apt-get update -y \ && apt-get install -y --no-install-recommends \ pkg-config libssl-dev ca-certificates ffmpeg \ && rm -rf /var/lib/apt/lists/* -RUN cargo build --release - -FROM python:3-slim AS runtime -FROM python:3.12-slim-trixie -COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/ - -ENV TZ=UTC -ENV RUST_LOG=info -ENV PATH=/home/app/.local/bin:$PATH - -RUN useradd --create-home --shell /bin/bash app -WORKDIR /home/app -USER app +RUN --mount=type=cache,target=/root/.cache/uv +# Intstall deps RUN uv tool install instaloader \ - && instaloader --version - -RUN uv tool install yt-dlp \ + && instaloader --version \ + && uv tool install yt-dlp[default] \ && yt-dlp --version -COPY --from=builder /app/target/release/tg-relay-rs /usr/local/bin/tg-relay-rs - +COPY --from=builder-rs /app/target/release/tg-relay-rs /usr/local/bin/tg-relay-rs CMD ["/usr/local/bin/tg-relay-rs"]