feat(docker): optimize build times

This commit is contained in:
Kristofers Solo 2025-09-23 09:43:08 +03:00
parent e21e73f0b4
commit e11223fa75
Signed by: kristoferssolo
GPG Key ID: 74FF8144483D82C8

View File

@ -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"]