Skip to content

Update memory-testing Dockerfile to use cargo-chef #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 20 additions & 30 deletions crates/memory-testing/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
###############################################
# Build stage #
###############################################
FROM rust:1.85 AS build

FROM rust:1.85 AS base
WORKDIR /app

# Copy dependency files and create dummy files to allow cargo to build the dependencies in a separate stage
COPY Cargo.toml Cargo.lock /app/
COPY crates/bitwarden-crypto/Cargo.toml /app/crates/bitwarden-crypto/
COPY crates/memory-testing/Cargo.toml /app/crates/memory-testing/

# Patch Cargo.toml to remove `, "bitwarden_license/*"` from the members array
RUN sed -i 's/\([" ]*\)"bitwarden_license\/\*"[ ,]*\([" ]*\)/\1\2/g' Cargo.toml

RUN mkdir -p /app/crates/bitwarden-crypto/src \
/app/crates/bitwarden-crypto/benches \
/app/crates/memory-testing/src \
&& touch /app/crates/bitwarden-crypto/src/lib.rs \
/app/crates/bitwarden-crypto/benches/default_allocator.rs \
/app/crates/bitwarden-crypto/benches/zeroizing_allocator.rs \
&& echo 'fn main(){}' > /app/crates/memory-testing/src/main.rs \
&& cargo build -p memory-testing --release

# Delete dummy files and copy the actual source code
RUN rm /app/crates/bitwarden-crypto/src/lib.rs \
/app/crates/bitwarden-crypto/benches/default_allocator.rs \
/app/crates/bitwarden-crypto/benches/zeroizing_allocator.rs \
/app/crates/memory-testing/src/main.rs

COPY crates/bitwarden-crypto /app/crates/bitwarden-crypto
COPY crates/memory-testing/src /app/crates/memory-testing/src

# Build the project. We use touch to force a rebuild of the now real files
RUN touch /app/crates/bitwarden-crypto/src/lib.rs /app/crates/memory-testing/src/main.rs
# Make sure the correct rust toolchain is installed only once
COPY rust-toolchain.toml /app/
RUN rustup show

# Install cargo-chef, to cache dependency builds
RUN cargo install cargo-chef --version 0.1.71 --locked

# Prepare the recipe for the dependencies, which will be built as the first part of the next stage
FROM base AS planner
COPY . /app/
RUN cargo chef prepare --recipe-path recipe.json

# Build dependencies based on the recipe of the previous stage, then copy the source and build the project
FROM base AS build
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook -p memory-testing --release --recipe-path recipe.json

# Copy the source here to reuse the cached dependencies
COPY . /app/
RUN cargo build -p memory-testing --release

###############################################
Expand Down
8 changes: 5 additions & 3 deletions crates/memory-testing/Dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
*
!crates/*
!Cargo.toml
!Cargo.lock
!/crates/*
!/bitwarden_license/*
!/Cargo.toml
!/Cargo.lock
!/rust-toolchain.toml