From 3306c3be88f59655d9b6c59de96d56fbc0417641 Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Thu, 8 Jul 2021 01:41:43 +0200 Subject: [PATCH] Optimize caching in Dockerfile --- Cargo.lock | 8 +++++--- Dockerfile | 52 ++++++++++++++++++++++++++++++++++++++++------- cli/Cargo.toml | 2 +- lib/Cargo.toml | 2 +- server/Cargo.toml | 2 +- 5 files changed, 53 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ad92a81..d59e3a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "addr2line" version = "0.14.1" @@ -2166,7 +2168,7 @@ checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" [[package]] name = "stl" -version = "2.6.4" +version = "2.6.5" dependencies = [ "bincode", "chrono", @@ -2189,7 +2191,7 @@ dependencies = [ [[package]] name = "stl_cli" -version = "2.6.4" +version = "2.6.5" dependencies = [ "chrono", "futures", @@ -2208,7 +2210,7 @@ dependencies = [ [[package]] name = "stl_lib" -version = "2.6.4" +version = "2.6.5" dependencies = [ "chrono", "serde", diff --git a/Dockerfile b/Dockerfile index 31eadc4..0e622e0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,18 +1,56 @@ +################## +### BASE STAGE ### +################## +FROM rust:1.53 as base + +# Install build dependencies +RUN cargo install strip_cargo_version +RUN rustup target add x86_64-unknown-linux-musl + +WORKDIR /app +RUN mkdir cli lib server + + +########################### +### STRIP-VERSION STAGE ### +########################### +FROM base as strip-version + +# Strip version from Cargo.* +# This avoids cache invalidations (ergo rebuilding all deps) when bumping the version number +COPY Cargo.lock Cargo.toml ./ +COPY server/Cargo.toml ./server/ +COPY cli/Cargo.toml ./cli/ +COPY lib/Cargo.toml ./lib/ +RUN strip_cargo_version + + ################### ### BUILD STAGE ### ################### -FROM rust:1.51 as build_stage +FROM base as build_stage -# Install build dependencies -RUN rustup target add x86_64-unknown-linux-musl +RUN cargo init --bin server +RUN cargo init --lib lib + +# Create a dummy binary for pre-compiling dependencies (for caching) +COPY --from=strip-version /app/server/Cargo.toml /app/server/ +COPY --from=strip-version /app/lib/Cargo.toml /app/lib/ +COPY --from=strip-version /app/cli/Cargo.toml /app/cli/ +COPY --from=strip-version /app/Cargo.lock /app/Cargo.toml /app/ -# Build project -WORKDIR /app -COPY . . WORKDIR /app/server RUN cargo build --release --target x86_64-unknown-linux-musl + +# Copy actual source files WORKDIR /app -RUN strip target/x86_64-unknown-linux-musl/release/stl +COPY . . + +# Compile final binary +WORKDIR /app/server +RUN cargo build --release --target x86_64-unknown-linux-musl +RUN strip /app/target/x86_64-unknown-linux-musl/release/stl + ######################## ### PRODUCTION STAGE ### diff --git a/cli/Cargo.toml b/cli/Cargo.toml index f2e0240..4295ed1 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "stl_cli" -version = "2.6.4" +version = "2.6.5" authors = ["Joakim Hulthe "] license = "MPL-2.0" edition = "2018" diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 30ebcd7..8c5c1ad 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "stl_lib" -version = "2.6.4" +version = "2.6.5" authors = ["Joakim Hulthe "] license = "MPL-2.0" edition = "2018" diff --git a/server/Cargo.toml b/server/Cargo.toml index d5d8650..e6c27bb 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "stl" description = "studielogg aka scuffed toggl" -version = "2.6.4" +version = "2.6.5" authors = ["Joakim Hulthe "] license = "MPL-2.0" edition = "2018"