Skip to content

Instantly share code, notes, and snippets.

@laundmo
Created April 12, 2023 19:22
Show Gist options
  • Save laundmo/a6557032236a84da93a13e8b69d26131 to your computer and use it in GitHub Desktop.
Save laundmo/a6557032236a84da93a13e8b69d26131 to your computer and use it in GitHub Desktop.
Rust dockerfile caching
FROM rust:1.65 as rust-builder
WORKDIR /usr/src/gt_bot
# Copy cargo
COPY ./Cargo.toml .
COPY ./Cargo.lock .
# Create fake main.rs file in src and build for dependencies
RUN mkdir ./src && echo 'fn main() { println!("Dummy!"); }' >./src/main.rs
RUN cargo build --release
# Copy source files over
RUN rm -rf ./src
COPY . .
# The last modified attribute of main.rs needs to be updated manually,
# otherwise cargo won't rebuild it.
RUN touch -a -m ./src/main.rs
RUN cargo build --release
# Actual final image with only the binary
FROM rust:1.65
COPY --from=rust-builder /usr/src/gt_bot/target/release/gt_bot /usr/local/bin/
WORKDIR /usr/local/bin
CMD ["gt_bot"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment