From caaaae7e88dbbb9f4a8d5f9151c42c4b26c5e3c4 Mon Sep 17 00:00:00 2001 From: Nazar Androshchuk Date: Thu, 20 Jun 2024 23:23:30 +0100 Subject: [PATCH 1/4] Dockerfile: Use cargo-chef - Better caching - Use `--workspace` instead of deprecated `--all` - Update to latest Rust 1 --- Dockerfile | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index e848041..014bd6a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,26 @@ -# ============ Build Stage ============ -FROM rust:1.74-bookworm as build - +FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef WORKDIR /typst-bot +FROM chef AS planner + # Compilation requires only the source code. COPY Cargo.toml Cargo.lock ./ COPY protocol protocol COPY worker worker COPY bot bot +RUN cargo chef prepare --recipe-path recipe.json -RUN cargo build --release --all --config git-fetch-with-cli=true +FROM chef AS builder +COPY --from=planner /typst-bot/recipe.json recipe.json +# Make sure the flags given are the same as in the `cargo build` line later on, so that +RUN cargo chef cook --release --workspace --recipe-path recipe.json +# Compilation requires only the source code. +COPY Cargo.toml Cargo.lock ./ +COPY protocol protocol +COPY worker worker +COPY bot bot +RUN cargo build --release --workspace --config git-fetch-with-cli=true # ============ Run Stage ============ FROM debian:bookworm-slim as run @@ -24,7 +34,7 @@ ENV DB_PATH=/bot/sqlite/db.sqlite \ CACHE_DIRECTORY=/bot/cache # The only files we need from the build stage in order to run the bot are the two executables. -COPY --from=build \ +COPY --from=builder \ /typst-bot/target/release/worker \ /typst-bot/target/release/typst-bot \ ./ @@ -32,3 +42,4 @@ COPY --from=build \ # Fonts are copied from the host at the very end so that the fonts can get updated without # invalidating any previously cached image layers. COPY fonts fonts + From 4077153046461ed1f6854518ada40ad9ec415e30 Mon Sep 17 00:00:00 2001 From: Nazar Androshchuk Date: Thu, 20 Jun 2024 23:35:06 +0100 Subject: [PATCH 2/4] Dockerfile: Update comments --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 014bd6a..e3168ac 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,4 @@ +# ============ Build Stage ============ FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef WORKDIR /typst-bot @@ -13,7 +14,7 @@ RUN cargo chef prepare --recipe-path recipe.json FROM chef AS builder COPY --from=planner /typst-bot/recipe.json recipe.json -# Make sure the flags given are the same as in the `cargo build` line later on, so that +# Caching layer RUN cargo chef cook --release --workspace --recipe-path recipe.json # Compilation requires only the source code. COPY Cargo.toml Cargo.lock ./ From 9574cae6d36226cea165292e3a85bb07184527b8 Mon Sep 17 00:00:00 2001 From: Nazar Androshchuk Date: Fri, 21 Jun 2024 12:15:21 +0100 Subject: [PATCH 3/4] Dockerfile: COPY in one layer --- Dockerfile | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index e3168ac..6ae68fe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,11 +4,7 @@ WORKDIR /typst-bot FROM chef AS planner -# Compilation requires only the source code. -COPY Cargo.toml Cargo.lock ./ -COPY protocol protocol -COPY worker worker -COPY bot bot +COPY --exclude=fonts . . RUN cargo chef prepare --recipe-path recipe.json FROM chef AS builder @@ -16,11 +12,7 @@ FROM chef AS builder COPY --from=planner /typst-bot/recipe.json recipe.json # Caching layer RUN cargo chef cook --release --workspace --recipe-path recipe.json -# Compilation requires only the source code. -COPY Cargo.toml Cargo.lock ./ -COPY protocol protocol -COPY worker worker -COPY bot bot +COPY --exclude=fonts . . RUN cargo build --release --workspace --config git-fetch-with-cli=true # ============ Run Stage ============ From 026e2c0e01e3dc46494ff56c4ec30212e37f9045 Mon Sep 17 00:00:00 2001 From: Tokarak <63452145+Tokarak@users.noreply.github.com> Date: Fri, 21 Jun 2024 12:54:11 +0100 Subject: [PATCH 4/4] Revert "Dockerfile: COPY in one layer" This reverts commit 9574cae6d36226cea165292e3a85bb07184527b8. COPY --exclude is not stable and unavailable in Podman --- Dockerfile | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6ae68fe..e3168ac 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,11 @@ WORKDIR /typst-bot FROM chef AS planner -COPY --exclude=fonts . . +# Compilation requires only the source code. +COPY Cargo.toml Cargo.lock ./ +COPY protocol protocol +COPY worker worker +COPY bot bot RUN cargo chef prepare --recipe-path recipe.json FROM chef AS builder @@ -12,7 +16,11 @@ FROM chef AS builder COPY --from=planner /typst-bot/recipe.json recipe.json # Caching layer RUN cargo chef cook --release --workspace --recipe-path recipe.json -COPY --exclude=fonts . . +# Compilation requires only the source code. +COPY Cargo.toml Cargo.lock ./ +COPY protocol protocol +COPY worker worker +COPY bot bot RUN cargo build --release --workspace --config git-fetch-with-cli=true # ============ Run Stage ============